MOOC哈工大2020C語言程序設計精髓練兵區編程題第十二周


1 大獎賽現場統分(4分)

題目內容:

已知某大獎賽有n個選手參賽,m(m>2)個評委為參賽選手評分(最高10分,最低0分)。統分規則為:在每個選手的m個得分中,去掉一個最高分和一個最低分后,取平均分作為該選手的最后得分。要求編程實現:

(1)根據n個選手的最后得分,從高到低輸出選手的得分名次表,以確定獲獎名單;

(2)根據各選手的最后得分與各評委給該選手所評分數的差距,對每個評委評分的准確性和評分水准給出一個定量的評價,從高到低輸出各評委得分的名次表。

提示:首先設計如下5個數組:

(1)sh[i],存放第i個選手的編號;

(2)sf[i],存放第i個選手的最后得分,即去掉一個最高分和一個最低分以后的平均分;

(3)ph[j],存放第j個評委的編號;

(4)f[i][j],存放第j個評委給第i個選手的評分;

(5)pf[j],存放代表第j個評委評分水准的得分。

解決本問題的關鍵在於計算選手的最后得分和評委的得分。

先計算選手的最后得分。外層循環控制參賽選手的編號i從1變化到n,當第i個選手上場時,輸入該選手的編號sh[i]。內層循環控制給選手評分的評委的編號j從1變化到m,依次輸入第j個評委給第i個選手的評分f[i][j],並將其累加到sf[i]中,同時求出最高分max和最低分min。當第i個選手的m個得分全部輸入並累加完畢后,去掉一個最高分max,去掉一個最低分min,於是第i個選手的最后得分為:

sf[i] = (sf[i] – max – min)/(m-2);

當n個參賽選手的最后得分sf[0],sf[1],…,sf[n]全部計算完畢后,再將其從高到低排序,打印參賽選手的名次表。

下面計算評委的得分。評委給選手評分存在誤差,即f[i][j]≠sf[i]是正常的,也是允許的。但如果某個評委給每個選手的評分與各選手的最后得分都相差太大,則說明該評委的評分有失水准。可用下面的公式來對各個評委的評分水平進行定量評價:

#define N 10

//找最大值,返回索引
int FindMax(float scores[], int n);

//找最小值,返回索引
int FindMin(float scores[], int n);

//計算最終分數
float CalculationFinalScore(float scores[], int n, int maxIndex, int minIndex);

//選手排序
void OrderAthletes(float sf[], int sh[], int n);

//評委排序
void OrderJudges(float f[][N], float sf[], float pf[], int ph[], int n, int m);

int main()
{
    int n, m, maxIndex, minIndex, sh[N], ph[N];
    float aver, scores[N], sf[N], pf[N], f[N][N], avers[N];
    printf("How many Athletes?\n");
    scanf("%d", &n);
    printf("How many judges?\n");
    scanf("%d", &m);
    printf("Scores of Athletes:\n");
    for (int k = 0; k < m; ++k)
    {
        ph[k] = k + 1;
    }
    //多少位選手
    for (int i = 0; i < n; i++)
    {
        printf("Athlete %d is playing.\n", i + 1);
        printf("Please enter his number code:\n");
        scanf("%d", &sh[i]);
        //評委
        for (int j = 0; j < m; ++j)
        {
            printf("Judge %d gives score:\n", j + 1);
            scanf("%f", &f[i][j]);
            scores[j] = f[i][j];
        }
        maxIndex = FindMax(scores, m);
        minIndex = FindMin(scores, m);
        printf("Delete a maximum score:%.1f\n", scores[maxIndex]);
        printf("Delete a minimum score:%.1f\n", scores[minIndex]);
        aver = CalculationFinalScore(scores, m, maxIndex, minIndex);
        printf("The final score of Athlete %d is %.3f\n", sh[i], aver);
        //平均分放進數組
        sf[i] = aver;
        avers[i] = aver;
    }
    printf("Order of Athletes:\n");
    OrderAthletes(sf, sh, n);
    printf("Order of judges:\n");
    OrderJudges(f, avers, pf, ph, n, m);
    printf("Over!Thank you!\n");
}

int FindMax(float scores[], int n)
{
    int max = 0;
    for (int i = 1; i < n; ++i)
    {
        if (scores[max] < scores[i])
        {
            max = i;
        }
    }
    return max;
}

int FindMin(float scores[], int n)
{
    int min = 0;
    for (int i = 1; i < n; ++i)
    {
        if (scores[min] > scores[i])
        {
            min = i;
        }
    }
    return min;
}

float CalculationFinalScore(float scores[], int n, int maxIndex, int minIndex)
{
    float sum = 0;
    for (int i = 0; i < n; ++i)
    {
        if (i == maxIndex || i == minIndex)
        {
            continue;
        }
        sum += scores[i];
    }
    return sum / (n - 2);
}

void OrderAthletes(float sf[], int sh[], int n)
{
    float temp;
    int flag, t;
    for (int i = 0; i < n; ++i)
    {
        flag = 0;
        for (int j = 0; j < n - i - 1; ++j)
        {
            if (sf[j] < sf[j + 1])
            {
                temp = sf[j + 1];
                sf[j + 1] = sf[j];
                sf[j] = temp;

                t = sh[j + 1];
                sh[j + 1] = sh[j];
                sh[j] = t;

                flag = 1;
            }
        }
        if (!flag)
        {
            break;
        }
    }
    printf("order\tfinal score\tnumber code\n");
    for (int k = 0; k < n; ++k)
    {
        printf("%5d\t%11.3f\t%6d\n", k + 1, sf[k], sh[k]);
    }
}

void OrderJudges(float f[][N], float sf[], float pf[], int ph[], int n, int m)
{
    int i, j, term1;
    float sum, term2;
    for (i = 0; i < m; i++)
    {
        sum = 0;
        for (j = 0; j < n; j++)
        {
            sum = sum + (f[j][i] - sf[j]) * (f[j][i] - sf[j]);
        }
        sum = sum / (float) n;
        pf[i] = 10 - sqrt(sum);
    }
    for (j = 0; j < n; j++)
    {
        for (i = 0; i < m - 1; i++)
        {
            if (pf[i] < pf[i + 1])
            {
                term1 = ph[i];
                ph[i] = ph[i + 1];
                ph[i + 1] = term1;
                term2 = pf[i];
                pf[i] = pf[i + 1];
                pf[i + 1] = term2;
            }
        }
    }
    printf("order\tfinal score\tnumber code\n");
    for (i = 0; i < m; i++)
    {
        printf("%5d\t%11.3f\t%6d\n", i + 1, pf[i], ph[i]);
    }
}

2 學生成績管理系統V3.0(4分)

題目內容:

某班有最多不超過30人(具體人數由鍵盤輸入)參加某門課程的考試,參考第11周在線測驗中“學生成績管理系統V2.0”,用二維字符數組作函數參數編程實現如下菜單驅動的學生成績管理系統:

(1)錄入每個學生的學號、姓名和考試成績;

(2)計算課程的總分和平均分;

(3)按成績由高到低排出名次表;

(4)按成績由低到高排出名次表;

(5)按學號由小到大排出成績表;

(6)按姓名的字典順序排出成績表;

(7)按學號查詢學生排名及其考試成績;

(8)按姓名查詢學生排名及其考試成績;

(9)按優秀(90~100)、良好(80~89)、中等(70~79)、及格(60~69)、不及格(0~59)5個類別,統計每個類別的人數以及所占的百分比;

(10)輸出每個學生的學號、姓名、考試成績。

要求程序運行后先顯示如下菜單,並提示用戶輸入選項:

1.Input record

2.Caculate total and average score of course

3.Sort in descending order by score

4.Sort in ascending order by score

5.Sort in ascending order by number

6.Sort in dictionary order by name

7.Search by number

8.Search by name

9.Statistic analysis

10.List record

0.Exit

Please enter your choice:

然后,根據用戶輸入的選項執行相應的操作。

請按照下面的定義及函數原型編程

#define   MAX_LEN  10         /* 字符串最大長度 */

#define   STU_NUM 30         /* 最多的學生人數 */

int   Menu(void);

void  ReadScore(long num[], char name[][MAX_LEN], float score[], int n);

void  AverSumofScore(float score[], int n);

void  SortbyScore(long num[], char name[][MAX_LEN], float score[], int n,

int (*compare)(float a, float b));

int   Ascending(float a, float b);

int   Descending(float a, float b);

void  SwapFloat(float *x, float *y);

void  SwapLong(long *x, long *y);

void  SwapChar(char x[], char y[]);

void  AsSortbyNum(long num[], char name[][MAX_LEN], float score[], int n);

void  SortbyName(long num[], char name[][MAX_LEN], float score[], int n);

void  SearchbyNum(long num[], char name[][MAX_LEN], float score[], int n);

void  SearchbyName(long num[], char name[][MAX_LEN], float score[], int n);

void  StatisticAnalysis(float score[], int n);

void  PrintScore(long num[], char name[][MAX_LEN], float score[], int n) ;

#include <string.h>
// 字符串最大長度
#define   MAX_LEN  10
// 最多的學生人數
#define   STU_NUM 30

int Menu(void);

void ReadScore(long num[], char name[][MAX_LEN], float score[], int n);

void AverSumofScore(float score[], int n);

void SortbyScore(long num[], char name[][MAX_LEN], float score[], int n, int (*compare)(float a, float b));

int Ascending(float a, float b);

int Descending(float a, float b);

void SwapFloat(float *x, float *y);

void SwapLong(long *x, long *y);

void SwapChar(char x[], char y[]);

void AsSortbyNum(long num[], char name[][MAX_LEN], float score[], int n);

void SortbyName(long num[], char name[][MAX_LEN], float score[], int n);

void SearchbyNum(long num[], char name[][MAX_LEN], float score[], int n);

void SearchbyName(long num[], char name[][MAX_LEN], float score[], int n);

void StatisticAnalysis(float score[], int n);

void PrintScore(long num[], char name[][MAX_LEN], float score[], int n);

int main()
{
    int n, m;
    long num[STU_NUM];
    float scores[STU_NUM];
    char name[STU_NUM][MAX_LEN];
    printf("Input student number(n<30):\n");
    scanf("%d", &n);
    Menu();
    while (scanf("%d", &m) && m != 0)
    {
        switch (m)
        {
            case 1:
                ReadScore(num, name, scores, n);
                break;
            case 2:
                AverSumofScore(scores, n);
                break;
            case 3:
                printf("Sort in descending order by score:\n");
                SortbyScore(num, name, scores, n, Descending);
                break;
            case 4:
                printf("Sort in ascending order by score:\n");
                SortbyScore(num, name, scores, n, Ascending);
                break;
            case 5:
                printf("Sort in ascending order by number:\n");
                AsSortbyNum(num, name, scores, n);
                break;
            case 6:
                printf("Sort in dictionary order by name:\n");
                SortbyName(num, name, scores, n);
                break;
            case 7:
                SearchbyNum(num, name, scores, n);
                break;
            case 8:
                SearchbyName(num, name, scores, n);
                break;
            case 9:
                StatisticAnalysis(scores, n);
                break;
            case 10:
                PrintScore(num, name, scores, n);
                break;
            default:
                printf("Input error!\n");
        }
        Menu();
    }
    printf("End of program!\n");
    return 0;
}

int Menu(void)
{
    printf("Management for Students' scores\n"
           "1.Input record\n"
           "2.Caculate total and average score of course\n"
           "3.Sort in descending order by score\n"
           "4.Sort in ascending order by score\n"
           "5.Sort in ascending order by number\n"
           "6.Sort in dictionary order by name\n"
           "7.Search by number\n"
           "8.Search by name\n"
           "9.Statistic analysis\n"
           "10.List record\n"
           "0.Exit\n"
           "Please Input your choice:\n");
    return 0;
}

void ReadScore(long num[], char name[][MAX_LEN], float score[], int n)
{
    printf("Input student's ID, name and score:\n");
    for (int i = 0; i < n; ++i)
    {
        scanf("%ld%s%f", &num[i], name[i], &score[i]);
    }
}

void AverSumofScore(float score[], int n)
{
    float sum = 0;
    for (int i = 0; i < n; ++i)
    {
        sum += score[i];
    }
    printf("sum=%.0f,aver=%.2f\n", sum, sum / n);
}

void SortbyScore(long num[], char name[][MAX_LEN], float score[], int n, int (*compare)(float a, float b))
{
    int flag;
    for (int i = 0; i < n; ++i)
    {
        flag = 0;
        for (int j = 0; j < n - i - 1; ++j)
        {
            if((*compare)(score[j], score[j + 1]))
            {
                SwapFloat(&score[j], &score[j + 1]);

                SwapLong(&num[j], &num[j + 1]);

                SwapChar(name[j], name[j + 1]);

                flag = 1;
            }
        }
        if(!flag)
        {
            break;
        }
    }
    PrintScore(num, name, score, n);
}

int  Ascending(float a, float b)
{
    return a > b ? 1 : 0;
}

int Descending(float a, float b)
{
    return a < b ? 1 : 0;
}

void SwapFloat(float *x, float *y)
{
    float temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

void SwapLong(long *x, long *y)
{
    long temp;
    temp = *x;
    *x = *y;
    *y = temp;
}

void SwapChar(char x[], char y[])
{
    char temp[MAX_LEN];
    strcpy(temp, x);
    strcpy(x, y);
    strcpy(y, temp);
}

void AsSortbyNum(long num[], char name[][MAX_LEN], float score[], int n)
{
    int flag;
    for (int i = 0; i < n; ++i)
    {
        flag = 0;
        for (int j = 0; j < n - i - 1; ++j)
        {
            if(num[j] > num[j + 1])
            {
                SwapLong(&num[j], &num[j + 1]);

                SwapFloat(&score[j], &score[j + 1]);

                SwapChar(name[j], name[j + 1]);

                flag = 1;
            }
        }
        if(!flag)
        {
            break;
        }
    }

    PrintScore(num, name, score, n);
}

void SortbyName(long num[], char name[][MAX_LEN], float score[], int n)
{
    int flag;
    for (int i = 0; i < n; ++i)
    {
        flag = 0;
        for (int j = 0; j < n - i - 1; ++j)
        {
            if(strcmp(name[j], name[j + 1]) > 0)
            {
                SwapLong(&num[j], &num[j + 1]);

                SwapFloat(&score[j], &score[j + 1]);

                SwapChar(name[j], name[j + 1]);

                flag = 1;
            }
        }
        if(!flag)
        {
            break;
        }
    }
    PrintScore(num, name, score, n);
}

void SearchbyNum(long num[], char name[][MAX_LEN], float score[], int n)
{
    long number;
    int flag = 0, i;
    printf("Input the number you want to search:\n");
    scanf("%ld", &number);
    for (i = 0; i < n; ++i)
    {
        if(number == num[i])
        {
            flag = 1;
            break;
        }
    }
    if(flag)
    {
        printf("%ld\t%s\t%.0f\n", num[i], name[i], score[i]);
    }
    else
    {
        printf("Not found!\n");
    }

}

void SearchbyName(long num[], char name[][MAX_LEN], float score[], int n)
{
    char str[MAX_LEN];
    int i, flag = 0;
    printf("Input the name you want to search:\n");
    scanf("%s", str);
    for (i = 0; i < n; ++i)
    {
        if(strcmp(name[i], str) == 0)
        {
            flag = 1;
            break;
        }
    }
    if(flag)
    {
        printf("%ld\t%s\t%.0f\n", num[i], name[i], score[i]);
    }
    else
    {
        printf("Not found!\n");
    }
}

void StatisticAnalysis(float scores[], int n)
{
    int a, b, c, d, e, f;
    a = b = c = d = e = f = 0;
    float score;
    for (int i = 0; i < n; ++i)
    {
        score = scores[i];
        if(score == 100)
        {
            a++;
        }
        else if(score >= 90 && score < 100 )
        {
            b++;
        }
        else if(score >= 80)
        {
            c++;
        }
        else if(score >= 70)
        {
            d++;
        }
        else if(score >= 60)
        {
            e++;
        }
        else
        {
            f++;
        }
    }
    printf("<60\t%d\t%.2f%%\n", f, (float)(100 * f) / n);
    printf("%d-%d\t%d\t%.2f%%\n" , 60, 69, e, (float)(100 * e) / n);
    printf("%d-%d\t%d\t%.2f%%\n" , 70, 79, d, (float)(100 * d) / n);
    printf("%d-%d\t%d\t%.2f%%\n" , 80, 89, c, (float)(100 * c) / n);
    printf("%d-%d\t%d\t%.2f%%\n" , 90, 99, b, (float)(100 * b) / n);
    printf("%d\t%d\t%.2f%%\n", 100, a,(float)(100 * a) / n);
}

void PrintScore(long num[], char name[][MAX_LEN], float score[], int n)
{
    for (int k = 0; k < n; ++k)
    {
        printf("%ld\t%s\t%.0f\n", num[k], name[k], score[k]);
    }
}

3 單詞接龍(4分)

題目內容:

阿泰和女友小菲用英語短信玩單詞接龍游戲。一人先寫一個英文單詞,然后另一個人回復一個英文單詞,要求回復單詞的開頭有若干個字母和上一個人所寫單詞的結尾若干個字母相同,重合部分的長度不限。(如阿泰輸入happy,小菲可以回復python,重合部分為py。)現在,小菲剛剛回復了阿泰一個單詞,阿泰想知道這個單詞與自己發過去的單詞的重合部分是什么。他們兩人都是喜歡寫長單詞的英語大神,阿泰覺得用肉眼找重合部分實在是太難了,所以請你編寫程序來幫他找出重合部分。

#define STR_LEN 80

int main()
{
    char word[STR_LEN + 1], word2[STR_LEN + 1], word3[STR_LEN + 1];
    int i = 0, j = 0, k = 0;
    scanf("%s%s", word, word2);

    while (word[i] != '\0')
    {
        if(word[i] == word2[0])
        {
            while (word2[j] != '\0')
            {
                if(word[i + j] != word2[j])
                {
                    break;
                }
                else
                {
                    word3[k] = word2[j];
                    k++;
                }
                j++;
            }
            word3[k] = '\0';
        }
        i++;
    }
    printf("%s\n", word3);
    return 0;
}

4 分數比較(4分)

題目內容:

比較兩個分數的大小。人工方式下比較分數大小最常見的方法是:進行分數的通分后比較分子的大小。可以編程模擬手工解決。

具體要求為首先輸出("Input two FENSHU:\n"),然后輸入兩個分數分子分母的值,格式為("%d/%d,%d/%d"),判斷完成后輸出("%d/%d<%d/%d\n")或("%d/%d>%d/%d\n")或("%d/%d=%d/%d\n");

int main()
{
    int a, b, c, d;
    printf("Input two FENSHU:\n");
    scanf("%d/%d,%d/%d", &a, &b, &c, &d);

    if(a * d > b * c)
    {
        printf("%d/%d>%d/%d\n", a, b, c, d);
    }
    else if(a * d == b * c)
    {
        printf("%d/%d=%d/%d\n", a, b, c, d);
    }
    else
    {
        printf("%d/%d<%d/%d\n", a, b, c, d);
    }
    return 0;
}

5 百萬富翁的換錢計划(4分)

題目內容:

有一天,一位百萬富翁遇到一個陌生人,陌生人找他談一個換錢的計划,陌生人對百萬富翁說:“我每天給你10萬元,而你第一天只需給我1分錢,第二天我仍給你10萬元,你給我2分錢,第三天我仍給你10萬元,你給我4分錢……。你每天給我的錢是前一天的兩倍,直到滿一個月(30天)為止”,百萬富翁很高興,欣然接受了這個契約。請編程計算在這一個月中陌生人總計給百萬富翁多少錢,百萬富翁總計給陌生人多少錢。程序中浮點數的數據類型均為double。

int main()
{
    double stranger = 0, richMan = 0;
    for (int i = 1; i <= 30; ++i)
    {
        richMan += 100000;
        stranger += pow(2, i - 1) * 0.01;
    }
    printf("to Stranger: %.2f yuan\n", stranger);
    printf("to Richman: %.2f yuan\n", richMan);
    return 0;
}

6 用計數控制的循環實現正數累加求和(4分)

題目內容:

輸入一些整數,編程計算並輸出其中所有正數的和,輸入負數時不累加,繼續輸入下一個數。輸入零時,表示輸入數據結束。要求最后統計出累加的項數。

int main()
{
    int count = 0, sum = 0, n;
    do
    {
        printf("Input a number:\n");
        scanf("%d", &n);
        if(n < 0)
        {
            continue;
        }
        sum += n;
        count++;
    }while (n != 0);
    printf("sum=%d,count=%d\n", sum, count - 1);
    return 0;
}

7 平方根表(4分)

題目內容:

輸出100(n^2<=100)以內整數的平方根表,n的值要求從鍵盤輸入,並且滿足n^2<=100 (即n的平方值在100以內)。

int main()
{
    int n;
    printf("Input n(n<=10):\n");
    scanf("%d", &n);
    if(n <= 10)
    {
        for (int i = 0; i < n; ++i)
        {
            printf("%7d", i);
        }
        printf("\n");
        for (int i = 0; i < n; ++i)
        {
            printf("%d", i);
            for (int j = 0; j < n; ++j)
            {
                printf("%7.3f", sqrt(i * 10 + j));
            }
            printf("\n");
        }
    }
    return 0;
}

8 最大公約數(4分)

題目內容:

按照如下函數原型編寫子函數計算正整數a和b的所有公約數。第一次調用,返回最大公約數。以后只要再使用相同參數調用,每次返回下一個小一些的公約數。無公約數時,函數CommonFactors()返回-1,主函數中不輸出任何信息。

函數原型: int CommonFactors(int a, int b)

int CommonFactors(int a, int b);

int n=1;
int main()
{
    int a, b, result, i;
    printf("Input a and b:\n");
    scanf("%d,%d", &a, &b);
    result = CommonFactors(a, b);
    if(a>0 && b>0)
    {
        for(i=1;result != -1;i++)
        {
            n++;
            printf("Common factor %d is %d\n", i, result);
            result = CommonFactors(a, b);
        }
    }
    return 0;
}

int CommonFactors(int a, int b)
{
    int i, count = 0;
    for (i = a; i >= 1; i--)
    {
        if (a % i == 0 && b % i == 0)
        {
            count++;
        }
        if (n == count)
            return i;
    }
    return -1;
}

9 23根火柴游戲(4分)

題目內容:

請編寫一個簡單的23 根火柴游戲程序,實現人跟計算機玩這個游戲的程序。為了方便程序自動評測,假設計算機移動的火柴數不是隨機的,而是將剩余的火柴根數對3求余后再加1來作為計算機每次取走的火柴數。如果剩余的火柴數小於3,則將剩余的火柴數減1作為計算機移走的火柴數。但是計算機不可以不取,剩下的火柴數為1時,必須取走1根火柴。假設游戲規則如下:

1)游戲者開始擁有23根火柴棒;

2)每個游戲者輪流移走1 根、2 根或3 根火柴;

3)誰取走最后一根火柴為失敗者。

int main()
{
    int n, m, sum = 23;
    printf("Game start!\n"
           "Note: the maximum number is 3\n");
    while (1)
    {
        printf("Please enter the number of matches you are moving:\n");
        scanf("%d", &n);
        if (n < 0 || n > 3)
        {
            printf("The number you entered is wrong,please re-enter!\n");
            continue;
        }
        else
        {
            printf("The number of matches you are moving is:%d\n", n);
            sum = sum - n;
            printf("The number of matches left is:%d\n", sum);
            if (sum == 0)
            {
                printf("I'm sorry. You lost!\n");
                break;
            }
            if (sum >= 3)
            {
                m = sum % 3 + 1;
            } 
            else if (sum > 1)
            {
                m = sum - 1;
            } 
            else
            {
                m = 1;
            }
            printf("The number of matches that have been moved by the computer is:%d\n", m);
            sum = sum - m;
            printf("The number of matches left is:%d\n", sum);
            if (sum == 0)
            {
                printf("Congratulations!You won!\n");
                break;
            }
        }
    }
    return 0;
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM