哈工大C語言程序設計精髓第十二週

由於這些代碼也是我初學時寫的代碼,故其中的規範程度及簡潔程度並不很好(此處我後來寫的有可以參考一下->C語言代碼規範),但是能很好的接近出初學者的水平,也更有參考價值!排版不易,喜歡就點個贊吧!如有問題,請勿吐槽,歡迎留言互相學習。

第12周編程題在線測試

  1. 計算時間差V2.0
    題目內容

    用結構體定義時鐘類型,編程從鍵盤任意輸入兩個時間(例如4時55分和1時25分),計算並輸出這兩個時間之間的間隔。要求不輸出時間差的負號。結構體類型定義如下:
typedef struct clock
{
    int hour;
    int minute;
    int second;
} CLOCK;

函數原型: CLOCK CalculateTime(CLOCK t1, CLOCK t2);
函數功能:計算返回兩個時間t1和t2之間的差
程序運行結果示例1:
Input time one:(hour,minute):4,55↙
Input time two: (hour,minute):1,25↙
3hour,30minute
程序運行結果示例2:
Input time one:(hour,minute):1,33↙
Input time two: (hour,minute):5,21↙
3hour,48minute
輸入提示: “Input time one:(hour,minute):”
“Input time two: (hour,minute):”
輸入格式: “%d,%d”
輸出格式:"%dhour,%dminute\n"

#include <stdio.h>
#include <string.h>
typedef struct clock
{
    int hour;
    int minute;
    int second;
} CLOCK;
 CLOCK CalculateTime(CLOCK t1, CLOCK t2);
int main()
{
    CLOCK t1,t2,t3;
    printf("Input time one:(hour,minute):");
    scanf("%d,%d",&t1.hour,&t1.minute);
    printf("Input time two: (hour,minute):");
    scanf("%d,%d",&t2.hour,&t2.minute);
    t3=CalculateTime(t1,t2);
    printf("%dhour,%dminute\n",t3.hour,t3.minute);
    return 0;
}
 CLOCK CalculateTime(CLOCK t1, CLOCK t2)
 {
     int sum1,sum2;
     CLOCK t3;
     sum1=t1.hour*60+t1.minute;
     sum2=t2.hour*60+t2.minute;
     if(sum1>sum2)
     {
         t3.hour=(sum1-sum2)/60;
         t3.minute=(sum1-sum2)%60;
     }
     else
     {
         t3.hour=(sum2-sum1)/60;
         t3.minute=(sum2-sum1)%60;
     }
     return t3;
 }
  1. 獎學金髮放
    題目內容

    某校的慣例是在每學期的期末考試之後發放獎學金。發放的獎學金共有五種,每項獎學金獲取的條件分別如下:
  1. 院士獎學金:期末平均成績高於80分(>80),並且在本學期內發表1篇或1篇以上論文的學生每人均可獲得8000元;
  2. 五四獎學金:期末平均成績高於85分(>85),並且班級評議成績高於80分(>80)的學生每人均可獲得4000元;
  3. 成績優秀獎:期末平均成績高於90分(>90)的學生每人均可獲得2000元;
  4. 西部獎學金:期末平均成績高於85分(>85)的西部省份學生每人均可獲得1000元;
  5. 班級貢獻獎:班級評議成績高於80分(>80)的學生幹部每人均可獲得850元;
    只要符合上述條件就可獲得相應的獎項,每項獎學金的獲獎人數沒有限制,每名學生也可以同時獲得多項獎學金。例如姚明的期末平均成績是87分,班級評議成績82分,同時他還是一位學生幹部,那麼他可以同時獲得五四獎學金和班級貢獻獎,獎金總數是4850元。
    現在給出若干學生的相關數據(假設總有同學能滿足獲得獎金的條件),請編程計算哪些同學獲得的獎金總數最高。
    結構體類型定義如下:
    typedef struct winners
    {
    char name[20];
    int finalScore;
    int classScore;
    char work;
    char west;
    int paper;
    int scholarship;
    } WIN;
    函數原型:void Addup(WIN stu[], int n);
    函數原型:int FindMax(WIN student[], int n);
    程序運行結果示例:
    Input n:4↙
    Input name:YaoMing↙
    Input final score:87↙
    Input class score:82↙
    Class cadre or not?(Y/N):Y↙
    Students from the West or not?(Y/N):N↙
    Input the number of published papers:0↙
    name:YaoMing,scholarship:4850
    Input name:ChenRuiyi↙
    Input final score:88↙
    Input class score:78↙
    Class cadre or not?(Y/N):N↙
    Students from the West or not?(Y/N):Y↙
    Input the number of published papers:1↙
    name:ChenRuiyi,scholarship:9000
    Input name:LiXin↙
    Input final score:92↙
    Input class score:88↙
    Class cadre or not?(Y/N):N↙
    Students from the West or not?(Y/N):N↙
    Input the number of published papers:0↙
    name:LiXin,scholarship:6000
    Input name:ZhangQin↙
    Input final score:83↙
    Input class score:87↙
    Class cadre or not?(Y/N):Y↙
    Students from the West or not?(Y/N):N↙
    Input the number of published papers:1↙
    name:ZhangQin,scholarship:8850
    ChenRuiyi get the highest scholarship 9000
    輸入學生人數提示:“Input n:”
    輸入學生姓名提示:“Input name:”
    輸入學生期末平均成績提示:“Input final score:”
    輸入學生班級評議成績提示:“Input class score:”
    輸入是否爲學生幹部提示:“Class cadre or not?(Y/N):”
    輸入是否爲西部學生提示:“Students from the West or not?(Y/N):”
    輸入發表文章數量提示:“Input the number of published papers:”
    輸入格式
    輸入學生人數:"%d"
    輸入學生姓名:"%s"
    輸入學生成績:"%d"
    輸入是否爲學生幹部:" %c" (注意:%c前面有一個空格)
    輸入是否爲西部學生:" %c" (注意:%c前面有一個空格)
    輸入發表文章數量: “%d”
    輸出格式
    輸出學生獲得的獎學金: “name:%s,scholarship:%d\n”
    輸出獲得獎學金總數最高的學生:"%s get the highest scholarship %d\n"
#include <stdio.h>
#include <string.h>
typedef struct winners
{
    char name[20];
    int finalScore;
    int classScore;
    char work;
    char west;
    int paper;
    int scholarship;
} WIN;
void Addup(WIN stu[], int n);
int FindMax(WIN student[], int n);
int main()
{
    int n,i,sum=0,max=0,m=0;
    WIN xue[10];
    printf("Input n:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        sum=0;
        printf("Input name:");
        scanf("%s",xue[i].name);
        printf("Input final score:");
        scanf("%d",&xue[i].finalScore);
        printf("Input class score:");
        scanf("%d",&xue[i].classScore);
        printf("Class cadre or not?(Y/N):");
        scanf(" %c",&xue[i].work);
        printf("Students from the West or not?(Y/N):");
        scanf(" %c",&xue[i].west);
        printf("Input the number of published papers:");
        scanf("%d",&xue[i].paper);
        if(xue[i].finalScore>80 && xue[i].paper>0)
        {
            sum+=8000;
        }
        if(xue[i].finalScore>85 && xue[i].classScore>80)
        {
            sum+=4000;
        }
        if(xue[i].finalScore>90)
        {
            sum+=2000;
        }
        if(xue[i].finalScore>85 && xue[i].west=='Y')
        {
            sum+=1000;
        }
        if(xue[i].finalScore>80 && xue[i].work=='Y')
        {
            sum+=850;
        }
        if(sum>max)
        {
            max=sum;
            m=i;
        }
        printf( "name:%s,scholarship:%d\n",xue[i].name,sum);
    }
    printf("%s get the highest scholarship %d\n",xue[m].name,max);
    return 0;
}
  1. 評選最牛羣主v1.0
    題目內容

    現在要評選最牛羣主,已知有3名最牛羣主的候選人(分別是tom,jack和rose),有不超過1000人蔘與投票,最後要通過投票評選出一名最牛羣主,從鍵盤輸入每位參與投票的人的投票結果,即其投票的候選人的名字,請你編程統計並輸出每位候選人的得票數,以及得票數最多的候選人的名字。候選人的名字中間不允許出現空格,並且必須小寫。若候選人名字輸入錯誤,則按廢票處理。
    程序運行結果示例1:
    Input the number of electorates:8↙
    Input vote 1:tom↙
    Input vote 2:jack↙
    Input vote 3:rose↙
    Input vote 4:tom↙
    Input vote 5:rose↙
    Input vote 6:rose↙
    Input vote 7:jack↙
    Input vote 8:rose↙
    Election results:
    tom:2
    jack:2
    rose:4
    rose wins
    程序運行結果示例2:
    Input the number of electorates:5↙
    Input vote 1:tom↙
    Input vote 2:mary↙
    Input vote 3:rose↙
    Input vote 4:jack↙
    Input vote 5:tom↙
    Election results:
    tom:2
    jack:1
    rose:1
    tom wins
    提示輸入候選人數量:“Input the number of electorates:”
    提示輸入候選人: “Input vote %d:”
    輸入格式:
    輸入候選人數量:"%d"
    輸入候選人姓名:"%s"
    輸出格式
    輸出候選人得票數:"%s:%d\n"
    輸出票數最多的候選人姓名:"%s wins\n"
    輸出評選結果提示信息:“Election results:\n”
#include <stdio.h>
#include <string.h>
int main()
{
    int n,i,p1,p2,p3,m=0;
    char name[1000][1000];
    printf("Input the number of electorates:");
    scanf("%d",&n);
    for(i=1,p1=p2=p3=0;i<=n;i++)
    {
        printf("Input vote %d:",i);
        scanf("%s",name[i]);
        if(name[i][0]=='t' && name[i][1]=='o' && name[i][2]=='m')
        {
            p1++;
            if(p1>m)
            {
                m=p1;
            }
        }
        else if(name[i][0]=='j' && name[i][1]=='a' && name[i][2]=='c' && name[i][3]=='k')
        {
            p2++;
            if(p2>m)
            {
                m=p2;
            }
        }
        else if(name[i][0]=='r' && name[i][1]=='o' && name[i][2]=='s' && name[i][3]=='e')
        {
            p3++;
            if(p3>m)
            {
                m=p3;
            }
        }
    }
    printf("Election results:\n");
    printf("%s:%d\n","tom",p1);
    printf("%s:%d\n","jack",p2);
    printf("%s:%d\n","rose",p3);
    if(m==p1)
    {
        printf("%s wins\n","tom");
    }
    else if(m==p2)
    {
        printf("%s wins\n","jack");
    }
    else if(m==p3)
    {
        printf("%s wins\n","rose");
    }
    return 0;
}
  1. 星期判斷
    題目內容

    請輸入星期幾的第一個字母(不區分大小寫)來判斷一下是星期幾,如果第一個字母一樣,則繼續判斷第二個字母(小寫),否則輸出“data error”。
    程序運行結果示例1:
    please input the first letter of someday:
    S↙
    please input second letter
    u↙
    sunday
    程序運行結果示例2:
    please input the first letter of someday:
    F↙
    friday
    程序運行結果示例2:
    please input the first letter of someday:
    h↙
    data error
    第一個字母的輸入提示信息:“please input the first letter of someday:\n”
    第二個字母的輸入提示信息:“please input second letter:\n”
    用戶輸入錯誤提示信息:“data error\n”
    輸入格式: " %c" (注意:%c前面有一個空格)
    輸出格式
    星期一:“monday\n”
    星期二:“tuesday\n”
    星期三:“wednesday\n”
    星期四:“thursday\n”
    星期五:“friday\n”
    星期六:“saturday\n”
    星期日:“sunday\n”
#include<stdio.h>
#include<string.h>
int main()
{
    char fir,sec;
    printf("please input the first letter of someday:\n");
    scanf(" %c",&fir);
    switch(fir)
    {
    case 's':
    case 'S':
        {
            printf("please input second letter:\n");
            scanf(" %c",&sec);
            if(sec=='a')
            {
                printf("saturday\n");
                break;
            }
            else if(sec=='u')
            {
                printf("sunday\n");
                break;
            }
            else
            {
                printf("data error\n");
                break;
            }
        }
    case 'm':
    case 'M':
        {
            printf("monday\n");
            break;
        }
    case 't':
    case 'T':
        {

            printf("please input second letter:\n");
            scanf(" %c",&sec);
            if(sec=='h')
            {
                printf("thursday\n");
                break;
            }
            else if(sec=='u')
            {
                printf("tuesday\n");
                break;
            }
            else
            {
                printf("data error\n");
                break;
            }
        }
    case 'w':
    case 'W':
        {
            printf("wednesday\n");
            break;
        }
    case 'f':
    case 'F':
        {
            printf("friday\n");
            break;
        }
    default:
        {
            printf("data error\n");
            break;
        }
    }
    return 0;
}

練兵區——編程題

  1. 大獎賽現場統分
    題目內容

    已知某大獎賽有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]是正常的,也是允許的。但如果某個評委給每個選手的評分與各選手的最後得分都相差太大,則說明該評委的評分有失水準。可用下面的公式來對各個評委的評分水平進行定量評價:
    程序的運行結果示例:
    How many Athletes?
    3↙
    How many judges?
    4↙
    Scores of Athletes:
    Athlete 1 is playing.
    Please enter his number code:
    101↙
    Judge 1 gives score:
    9.8↙
    Judge 2 gives score:
    9.7↙
    Judge 3 gives score:
    9.5↙
    Judge 4 gives score:
    9.1↙
    Delete a maximum score:9.8
    Delete a minimum score:9.1
    The final score of Athlete 101 is 9.600
    Athlete 2 is playing.
    Please enter his number code:
    102↙
    Judge 1 gives score:
    8.9↙
    Judge 2 gives score:
    8.1↙
    Judge 3 gives score:
    8.6↙
    Judge 4 gives score:
    8.4↙
    Delete a maximum score:8.9
    Delete a minimum score:8.1
    The final score of Athlete 102 is 8.500
    Athlete 3 is playing.
    Please enter his number code:
    103↙
    Judge 1 gives score:
    9.0↙
    Judge 2 gives score:
    9.5↙
    Judge 3 gives score:
    9.4↙
    Judge 4 gives score:
    9.2↙
    Delete a maximum score:9.5
    Delete a minimum score:9.0
    The final score of Athlete 103 is 9.300
    Order of Athletes:
    order final score number code
    1 9.600 101
    2 9.300 103
    3 8.500 102
    Order of judges:
    order final score number code
    1 9.900 3
    2 9.735 2
    3 9.700 4
    4 9.689 1
    Over!Thank you!
    程序中浮點數的數據類型均爲float。
    輸入選手人數提示信息:“How many Athletes?\n”
    輸入評委人數提示信息:“How many judges?\n”
    輸入選手編號提示信息:“Please enter his number code:\n”
    輸入格式:
    評委人數、選手人數、選手編號:"%d"
    評委打分:"%f"
    輸出格式
    選手得分提示信息:“Scores of Athletes:\n”
    當前選手提示信息:“Athlete %d is playing.\n”
    評委打分提示信息:“Judge %d gives score:\n”
    去掉最高分:“Delete a maximum score:%.1f\n”
    去掉最低分:“Delete a minimum score:%.1f\n”
    選手最後得分提示信息:“The final score of Athlete %d is %.3f\n”
    選手得分排序提示信息:“Order of Athletes:\n”
    評委排序提示信息:“Order of judges:\n”
    選手/評委 排序表頭提示信息都是:“order\tfinal score\tnumber code\n”
    選手/評委 得分排序輸出格式都是:"%5d\t%11.3f\t%6d\n"
    評委排序表頭提示信息: “order\tfinal score\tnumber code\n”
    評分結束提示信息: “Over!Thank you!\n”
#include<stdio.h>
#include<string.h>
#include <math.h>
#define N 10
int main()
{
    int sh[N],ph[N],n,m,i,y,temp2;
    float f[N][N],pf[N],sf[N],max=0,min=10,temp1,sum=0;
    printf("How many Athletes?\n");
    scanf("%d",&n);
    printf("How many judges?\n");
    scanf("%d",&m);
    printf("Scores of Athletes:\n");
    for(i=1;i<=n;i++)
    {
        sf[i]=0,max=0,min=10;
        printf("Athlete %d is playing.\n",i);
        printf("Please enter his number code:\n");
        scanf("%d",&sh[i]);
        for(y=1;y<=m;y++)
        {
            printf("Judge %d gives score:\n",y);
            scanf("%f",&f[i][y]);
            sf[i]+=f[i][y];
            if(f[i][y]<min)
            {
                min=f[i][y];
            }
            if(f[i][y]>max)
            {
                max=f[i][y];
            }
        }
        sf[i] = (sf[i] - max - min)/(m-2);
        printf("Delete a maximum score:%.1f\n",max);
        printf("Delete a minimum score:%.1f\n",min);
        printf("The final score of Athlete %d is %.3f\n",sh[i],sf[i]);
    }
    printf("Order of Athletes:\n");
    printf("order\tfinal score\tnumber code\n");
    for(y=1;y<=m;y++)
    {
        sum=0;
        for(i=1;i<=n;i++)
        {
            sum+=pow(f[i][y]-sf[i],2);
        }
        sum/=(float)n;
        pf[y]=10-sqrt(sum);
        ph[y]=y;
    }
    for(i=1;i<=n;i++)
    {
        for(y=i+1;y<=n;y++)
        {
            if(sf[i]<sf[y])
            {
                temp1=sf[i];
                temp2=sh[i];
                sf[i]=sf[y];
                sh[i]=sh[y];
                sf[y]=temp1;
                sh[y]=temp2;
            }
        }
    }
    for(i=1;i<=n;i++)
    {
        printf("%5d\t%11.3f\t%6d\n",i,sf[i],sh[i]);
    }
    printf("Order of judges:\n");
    printf("order\tfinal score\tnumber code\n");
    for(i=1;i<=m;i++)
    {
        max=0,min=10;
        for(y=i+1;y<=m;y++)
        {
            if(pf[i]<pf[y])
            {
                temp1=pf[i];
                temp2=ph[i];
                pf[i]=pf[y];
                ph[i]=ph[y];
                pf[y]=temp1;
                ph[y]=temp2;
            }
        }
    }
    for(i=1;i<=m;i++)
    {
        printf("%5d\t%11.3f\t%6d\n",i,pf[i],ph[i]);
    }
    printf("Over!Thank you!\n");
    return 0;
}
  1. 學生成績管理系統V3.0
    題目內容

    某班有最多不超過30人(具體人數由鍵盤輸入)參加某門課程的考試,參考第11周在線測驗中“學生成績管理系統V2.0”,用二維字符數組作函數參數編程實現如下菜單驅動的學生成績管理系統:
    (1)錄入每個學生的學號、姓名和考試成績;
    (2)計算課程的總分和平均分;
    (3)按成績由高到低排出名次表;
    (4)按成績由低到高排出名次表;
    (5)按學號由小到大排出成績表;
    (6)按姓名的字典順序排出成績表;
    (7)按學號查詢學生排名及其考試成績;
    (8)按姓名查詢學生排名及其考試成績;
    (9)按優秀(90100)、良好(8089)、中等(7079)、及格(6069)、不及格(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) ;

程序運行結果示例:
Input student number(n<30):
6↙
Management for Students’ scores
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 Input your choice:
1↙
Input student’s ID, name and score:
11003001↙
lisi↙
87↙
11003005↙
heli↙
98↙
11003003↙
ludi↙
75↙
11003002↙
dumo↙
48↙
11003004↙
zuma↙
65↙
11003006↙
suyu↙
100↙

Management for Students’ scores
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 Input your choice:
2↙
sum=473,aver=78.83
Management for Students’ scores
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 Input your choice:
3↙
Sort in descending order by score:
11003006 suyu 100
11003005 heli 98
11003001 lisi 87
11003003 ludi 75
11003004 zuma 65
11003002 dumo 48
Management for Students’ scores
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 Input your choice:
4↙
Sort in ascending order by score:
11003002 dumo 48
11003004 zuma 65
11003003 ludi 75
11003001 lisi 87
11003005 heli 98
11003006 suyu 100
Management for Students’ scores
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 Input your choice:
5↙
Sort in ascending order by number:
11003001 lisi 87
11003002 dumo 48
11003003 ludi 75
11003004 zuma 65
11003005 heli 98
11003006 suyu 100
Management for Students’ scores
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 Input your choice:
6↙
Sort in dictionary order by name:
11003002 dumo 48
11003005 heli 98
11003001 lisi 87
11003003 ludi 75
11003006 suyu 100
11003004 zuma 65
Management for Students’ scores
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 Input your choice:
7↙
Input the number you want to search:
11003004↙
11003004 zuma 65
Management for Students’ scores
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 Input your choice:
8↙
Input the name you want to search:
heli↙
11003005 heli 98
Management for Students’ scores
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 Input your choice:
9↙
<60 1 16.67%
60-69 1 16.67%
70-79 1 16.67%
80-89 1 16.67%
90-99 1 16.67%
100 1 16.67%
Management for Students’ scores
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 Input your choice:
10↙
11003002 dumo 48
11003005 heli 98
11003001 lisi 87
11003003 ludi 75
11003006 suyu 100
11003004 zuma 65
Management for Students’ scores
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 Input your choice:
11↙
Input error!
Management for Students’ scores
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 Input your choice:
0↙
End of program!
輸入格式:
( 1 ) 錄入學生的人數:
要求輸入數據格式爲:"%d"
提示信息爲:“Input student number(n<30):\n”
( 2 )錄入每個學生的學號、姓名和考試成績:
要求輸入數據格式爲:"%ld%s%f"
提示信息爲:“Input student’s ID, name and score:\n”
輸出格式
計算課程的總分和平均分:
要求輸出總分與平均分格式爲:“sum=%.0f,aver=%.2f\n”
按成績由高到低排出名次表:
要求輸出格式爲:"%ld\t%s\t%.0f\n"
提示信息爲:“Sort in descending order by score:\n”
按成績由低到高排出名次表:
要求輸出格式爲:"%ld\t%s\t%.0f\n"
提示信息爲:“Sort in ascending order by score:\n”
按學號由小到大排出成績表:
要求輸出格式爲:"%ld\t%s\t%.0f\n"
提示信息爲:“Sort in ascending order by number:\n”
按姓名的字典順序排出成績表
要求輸出格式爲:"%ld\t%s\t%.0f\n"
提示信息爲:“Sort in dictionary order by name:\n”
按學號查詢學生排名及其考試成績:
如果未查到此學號的學生,提示信息爲:“Not found!\n”;
如果查詢到該學生,要求輸出格式爲:"%ld\t%s\t%.0f\n"
按姓名查詢學生排名及其考試成績;
如果未查到此學號的學生,提示信息爲:“Not found!\n”;
如果查詢到該學生,要求輸出格式爲:"%ld\t%s\t%.0f\n"
按優秀(90100)、良好(8089)、中等(7079)、及格(6069)、不及格(0~59)5個類別,統計每個類別的人數以及所佔的百分比:
成績<60輸出提示格式爲:"<60\t%d\t%.2f%%\n";
成績=100輸出格式爲:"%d\t%d\t%.2f%%\n";
其他要求輸出百分比格式爲:"%d-%d\t%d\t%.2f%%\n"
輸出每個學生的學號、姓名、考試成績,以及課程總分和平均分
輸出格式爲:"%ld\t%s\t%.0f\n"
選擇退出(菜單項0)
提示信息:“End of program!”
菜單項選擇錯誤(不在0-10之間)
提示信息:“Input error!\n”

#include <stdio.h>
#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()
{
    long num[STU_NUM];
    float score[STU_NUM];
    char name[STU_NUM][MAX_LEN];
    int n,co;
    printf("Input student number(n<30):\n");
    scanf("%d",&n);
    while(1)
    {
        co=Menu();
        switch(co)
        {
        case 1:
            ReadScore(num,name,score,n);
            break;
        case 2:
            AverSumofScore(score,n);
            break;
        case 3:
            SortbyScore(num,name,score,n,Descending);
            printf("Sort in descending order by score:\n");
            PrintScore(num,name,score,n);
            break;
        case 4:
            SortbyScore(num,name,score,n,Ascending);
            printf("Sort in ascending order by score:\n");
            PrintScore(num,name,score,n);
            break;
        case 5:
            AsSortbyNum(num,name,score,n);
            printf("Sort in ascending order by number:\n");
            PrintScore(num,name,score,n);
            break;
        case 6:
            SortbyName(num,name,score,n);
            printf("Sort in dictionary order by name:\n");
            PrintScore(num,name,score,n);
            break;
        case 7:
            SearchbyNum(num,name,score,n);
            break;
        case 8:
            SearchbyName(num,name,score,n);
            break;
        case 9:
            StatisticAnalysis(score,n);
            break;
        case 10:
            PrintScore(num,name,score,n);
            break;
        case 0:
            printf("End of program!");
            exit(0);
        default :
            printf("Input error!\n");
        }
    }
}
int   Menu(void)
{
    int co;
    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");
    scanf("%d",&co);
    return co;
}
void  ReadScore(long num[], char name[][MAX_LEN], float score[], int n)
{
    int i;
    printf("Input student's ID, name and score:\n");
    for(i=0;i<n;i++)
    {
        scanf("%ld%s%f",&num[i],name[i],&score[i]);
    }
}
void  AverSumofScore(float score[], int n)
{
    float sum=0;
    int i;
    for(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 i,j;
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if((*compare)(score[i],score[j]))
            {
                SwapFloat(&score[i],&score[j]);
                SwapChar(name[i],name[j]);
                SwapLong(&num[i],&num[j]);
            }
        }
    }
}
int   Ascending(float a, float b)
{
    return a>b;
}
int   Descending(float a, float b)
{
    return a<b;
}
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 i,j;
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(num[i]>num[j])
            {
                SwapFloat(&score[i],&score[j]);
                SwapChar(name[i],name[j]);
                SwapLong(&num[i],&num[j]);
            }
        }
    }
}
void  SortbyName(long num[], char name[][MAX_LEN], float score[], int n)
{
    int i,j;
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(strcmp(name[i],name[j])>0)
            {
                SwapFloat(&score[i],&score[j]);
                SwapChar(name[i],name[j]);
                SwapLong(&num[i],&num[j]);
            }
        }
    }
}
void  SearchbyNum(long num[], char name[][MAX_LEN], float score[], int n)
{
    long se;
    int i,t=0;
    printf("Input the number you want to search:\n");
    scanf("%ld",&se);
    for(i=0;i<n;i++)
    {
        if(num[i]==se)
        {
            printf("%ld\t%s\t%.0f\n",num[i],name[i],score[i]);
            t=1;
        }
    }
    if(t==0)
    {
        printf("Not found!\n");
    }
}
void  SearchbyName(long num[], char name[][MAX_LEN], float score[], int n)
{
    char se[MAX_LEN];
    int i,t=0;
    printf("Input the name you want to search:\n");
    scanf("%s",se);
    for(i=0;i<n;i++)
    {
        if(strcmp(se,name[i])==0)
        {
            printf("%ld\t%s\t%.0f\n",num[i],name[i],score[i]);
            t=1;
        }
    }
    if(t==0)
    {
        printf("Not found!\n");
    }
}
void  StatisticAnalysis(float score[], int n)
{
    int d1,d2,d3,d4,d5,d6,i;
    for(i=0,d1=d2=d3=d4=d5=d6=0;i<n;i++)
    {
        if(score[i]<60){
            d1++;}
        else if(score[i]<70){
            d2++;}
        else if(score[i]<80){
            d3++;}
        else if(score[i]<90){
            d4++;}
        else if(score[i]<100){
            d5++;}
        else{
            d6++;}
    }
    printf("<60\t%d\t%.2f%%\n",d1,100*(float)d1/n);
    printf("%d-%d\t%d\t%.2f%%\n",60,69,d2,100*(float)d2/n);
    printf("%d-%d\t%d\t%.2f%%\n",70,79,d3,100*(float)d3/n);
    printf("%d-%d\t%d\t%.2f%%\n",80,89,d4,100*(float)d4/n);
    printf("%d-%d\t%d\t%.2f%%\n",90,99,d5,100*(float)d5/n);
    printf("%d\t%d\t%.2f%%\n",100,d6,100*    (float)d6/n);
}
void  PrintScore(long num[], char name[][MAX_LEN], float score[], int n)
{
    int i;
    for(i=0;i<n;i++)
    {
        printf("%ld\t%s\t%.0f\n",num[i],name[i],score[i]);
    }
}
  1. 單詞接龍
    題目內容

    阿泰和女友小菲用英語短信玩單詞接龍遊戲。一人先寫一個英文單詞,然後另一個人回覆一個英文單詞,要求回覆單詞的開頭有若干個字母和上一個人所寫單詞的結尾若干個字母相同,重合部分的長度不限。(如阿泰輸入happy,小菲可以回覆python,重合部分爲py。)現在,小菲剛剛回復了阿泰一個單詞,阿泰想知道這個單詞與自己發過去的單詞的重合部分是什麼。他們兩人都是喜歡寫長單詞的英語大神,阿泰覺得用肉眼找重合部分實在是太難了,所以請你編寫程序來幫他找出重合部分。
    程序運行結果示例1:
    happy↙
    pythen↙
    py
    程序運行結果示例2:
    sun↙
    unknown↙
    un
    輸入格式: “%s%s”
    輸出格式: “%s\n”
#include <string.h>
#include<stdio.h>
int main()
{
    char at[100], xf[100],ch[100];
    int i, j, y, t=0;
    scanf("%s%s", at, xf);
    for(i=0; i<strlen(at); i++)
    {
        for(j=0, y=i; j<strlen(xf) && y<strlen(at); y++, j++)
        {
            if(xf[j]!=at[y])
            {
                t=0;
                break;
            }
            t=1;
        }
        if(t==1)
        {
            for(y=i, j=0 ; y<strlen(at); y++, j++)
            {
                ch[j]=at[y];
            }
            ch[j]='\0';
            printf("%s\n",ch);
            break;
        }
    }
    return 0;
}
  1. 分數比較
    題目內容

    比較兩個分數的大小。人工方式下比較分數大小最常見的方法是:進行分數的通分後比較分子的大小。可以編程模擬手工解決。
    具體要求爲首先輸出(“Input two FENSHU:\n”),然後輸入兩個分數分子分母的值,格式爲("%d/%d,%d/%d"),判斷完成後輸出("%d/%d<%d/%d\n")或("%d/%d>%d/%d\n")或("%d/%d=%d/%d\n");
    程序運行結果示例1:
    Input two FENSHU:
    17/19,23/27↙
    17/19>23/27
    程序運行結果示例2:
    Input two FENSHU
    2/3,2/3↙
    2/3=2/3
    程序運行結果示例3:
    Input two FENSHU:
    1/7,1/2↙
    1/7<1/2
    輸入提示信息:“Input two FENSHU:\n”
    輸入格式: “%d/%d,%d/%d”
    輸出格式
    如果前者大於後者輸出提示信息:"%d/%d>%d/%d\n"
    如果前者等於後者輸出提示信息:"%d/%d=%d/%d\n"
    如果前者小於後者輸出提示信息:"%d/%d<%d/%d\n"
#include <string.h>
#include<stdio.h>
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;
}
  1. 百萬富翁的換錢計劃
    題目內容

    有一天,一位百萬富翁遇到一個陌生人,陌生人找他談一個換錢的計劃,陌生人對百萬富翁說:“我每天給你10萬元,而你第一天只需給我1分錢,第二天我仍給你10萬元,你給我2分錢,第三天我仍給你10萬元,你給我4分錢……。你每天給我的錢是前一天的兩倍,直到滿一個月(30天)爲止”,百萬富翁很高興,欣然接受了這個契約。請編程計算在這一個月中陌生人總計給百萬富翁多少錢,百萬富翁總計給陌生人多少錢。程序中浮點數的數據類型均爲double。
    輸入格式: 無
    輸出格式
    輸出百萬富翁給陌生人的錢: “to Stranger: %.2f yuan\n”
    輸出陌生人給百萬富翁的錢: “to Richman: %.2f yuan\n”
#include <string.h>
#include<stdio.h>
int main()
{
    int i;
    double a=0.01,sum1=0,sum2=0;
    for(i=1;i<=30;i++)
    {
        sum1+=100000;
        sum2+=a;
        a*=2;
    }
    printf("to Stranger: %.2f yuan\n",sum2);
    printf("to Richman: %.2f yuan\n",sum1);
    return 0;
}
  1. 用計數控制的循環實現正數累加求和
    題目內容

    輸入一些整數,編程計算並輸出其中所有正數的和,輸入負數時不累加,繼續輸入下一個數。輸入零時,表示輸入數據結束。要求最後統計出累加的項數。
    程序運行結果示例:
    Input a number:
    1↙
    Input a number:
    3↙
    Input a number:
    4↙
    Input a number:
    2↙
    Input a number:
    -8↙
    Input a number:
    -9↙
    Input a number:
    0↙
    sum=10,count=4
    輸入提示信息: “Input a number:\n”
    輸入格式: “%d”
    輸出格式: “sum=%d,count=%d\n”
#include <string.h>
#include<stdio.h>
int main()
{
    int a=1,t=0,sum=0;
    while(a!=0)
    {
        printf( "Input a number:\n");
        scanf("%d",&a);
        if(a>0)
        {
            sum+=a;
            t++;
        }
    }
    printf("sum=%d,count=%d\n",sum,t);
    return 0;
}
  1. 平方根表
    題目內容

    輸出100(n2<=100)以內整數的平方根表,n的值要求從鍵盤輸入,並且滿足n2<=100 (即n的平方值在100以內)。
    程序運行結果示例:
    輸入提示:“Input n(n<=10):\n”
    輸入格式: “%d”
    輸出格式
    輸出表頭: “%7d”
    輸出每行的開頭數字: “%d”
    輸出第m行n列中的值:"%7.3f"
#include <string.h>
#include<stdio.h>
#include <math.h>
int main()
{
    int n,i,j;
    double sq;
    printf("Input n(n<=10):\n");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("%7d",i);
    }
    printf("\n");
    for(i=0;i<n;i++)
    {
        printf("%d",i);
        for(j=0;j<n;j++)
        {
            sq=sqrt(i*10.0+j);
            printf("%7.3f",sq);
        }
        printf("\n");
    }
    return 0;
}
  1. 最大公約數
    題目內容

    按照如下函數原型編寫子函數計算正整數a和b的所有公約數。第一次調用,返回最大公約數。以後只要再使用相同參數調用,每次返回下一個小一些的公約數。無公約數時,函數CommonFactors()返回-1,主函數中不輸出任何信息。
    函數原型: int CommonFactors(int a, int b)
    程序運行結果示例1:
    Input a and b:
    100,50↙
    Common factor 1 is 50
    Common factor 2 is 25
    Common factor 3 is 10
    Common factor 4 is 5
    Common factor 5 is 2
    Common factor 6 is 1
    程序運行結果示例2:
    Input a and b:
    7,-3↙
    輸入提示信息:“Input a and b:\n”
    輸入格式: “%d,%d”
    輸出格式: “Common factor %d is %d\n”
#include<stdio.h>
int CommonFactors(int a, int b);
int n = 1;
int main()
{
    int a, b;
    int ret;
    printf("Input a and b:\n");
    scanf("%d,%d", &a, &b);
    ret = CommonFactors(a,b);
    if(a > 0 && b > 0)
    {
        while(ret > 0)
        {
            printf("Common factor %d is %d\n", n, ret);
            n++;
            ret = CommonFactors(a,b);
        }
    }

    return 0;
}
int CommonFactors(int a, int b)
{
    int i, cnt = 0;
    int temp;
    if(a < b)
    {
        temp = a;
        a = b;
        b = temp;
    }
    for(i = a; i >= 1; i--)
    {
        if(a%i == 0 && b%i == 0)
            cnt++;
        if(n == cnt)
        return i;
    }
    return -1;
}
  1. 23根火柴遊戲
    題目內容

    請編寫一個簡單的23 根火柴遊戲程序,實現人跟計算機玩這個遊戲的程序。爲了方便程序自動評測,假設計算機移動的火柴數不是隨機的,而是將剩餘的火柴根數對3求餘後再加1來作爲計算機每次取走的火柴數。如果剩餘的火柴數小於3,則將剩餘的火柴數減1作爲計算機移走的火柴數。但是計算機不可以不取,剩下的火柴數爲1時,必須取走1根火柴。假設遊戲規則如下:
    1)遊戲者開始擁有23根火柴棒;
    2)每個遊戲者輪流移走1 根、2 根或3 根火柴;
    3)誰取走最後一根火柴爲失敗者。
    程序運行結果示例1:
    Game start!
    Note: the maximum number is 3
    Please enter the number of matches you are moving:
    5↙
    The number you entered is wrong,please re-enter!
    Please enter the number of matches you are moving:
    3↙
    The number of matches you are moving is:3
    The number of matches left is:20
    The number of matches that have been moved by the computer is:3
    The number of matches left is:17
    Please enter the number of matches you are moving:
    1↙
    The number of matches you are moving is:1
    The number of matches left is:16
    The number of matches that have been moved by the computer is:2
    The number of matches left is:14
    Please enter the number of matches you are moving:
    2↙
    The number of matches you are moving is:2
    The number of matches left is:12
    The number of matches that have been moved by the computer is:1
    The number of matches left is:11
    Please enter the number of matches you are moving:
    3↙
    The number of matches you are moving is:3
    The number of matches left is:8
    The number of matches that have been moved by the computer is:3
    The number of matches left is:5
    Please enter the number of matches you are moving:
    1↙
    The number of matches you are moving is:1
    The number of matches left is:4
    The number of matches that have been moved by the computer is:2
    The number of matches left is:2
    Please enter the number of matches you are moving:
    1↙
    The number of matches you are moving is:1
    The number of matches left is:1
    The number of matches that have been moved by the computer is:1
    The number of matches left is:0
    Congratulations!You won!
    程序運行結果示例2:
    Game start!
    Note: the maximum number is 3
    Please enter the number of matches you are moving:
    3↙
    The number of matches you are moving is:3
    The number of matches left is:20
    The number of matches that have been moved by the computer is:3
    The number of matches left is:17
    Please enter the number of matches you are moving:
    3↙
    The number of matches you are moving is:3
    The number of matches left is:14
    The number of matches that have been moved by the computer is:3
    The number of matches left is:11
    Please enter the number of matches you are moving:
    2↙
    The number of matches you are moving is:2
    The number of matches left is:9
    The number of matches that have been moved by the computer is:1
    The number of matches left is:8
    Please enter the number of matches you are moving:
    1↙
    The number of matches you are moving is:1
    The number of matches left is:7
    The number of matches that have been moved by the computer is:2
    The number of matches left is:5
    Please enter the number of matches you are moving:
    3↙
    The number of matches you are moving is:3
    The number of matches left is:2
    The number of matches that have been moved by the computer is:1
    The number of matches left is:1
    Please enter the number of matches you are moving:
    1↙
    The number of matches you are moving is:1
    The number of matches left is:0
    I’m sorry. You lost!
    遊戲開始提示信息:“Game start!\n”
    “Note: the maximum number is 3\n”
    提示遊戲者輸入移動的火柴數:“Please enter the number of matches you are moving:\n”
    遊戲者輸入錯誤數據的提示信息:“The number you entered is wrong,please re-enter!\n”
    輸入格式: “%d”
    輸出格式
    輸出被遊戲者移動的火柴數:“The number of matches you are moving is:%d\n”
    輸出被計算機移動的火柴數:“The number of matches that have been moved by the computer is:%d\n”
    輸出被遊戲者或計算機移動後剩餘的火柴數:“The number of matches left is:%d\n”
    遊戲者獲勝的輸出提示信息:“Congratulations!You won!\n”
    遊戲者失敗的輸出提示信息:“I’m sorry. You lost!\n”
#include <stdio.h>
#include <string.h>
int main()
{
    int n,m,sum=23;
    printf("Game start!\n");
    printf("Note: the maximum number is 3\n");
    while(sum!=0)
    {
        printf("Please enter the number of matches you are moving:\n");
        scanf("%d",&n);
        if(n>0 && n<4)
        {
            printf("The number of matches you are moving is:%d\n",n);
            sum-=n;
            printf("The number of matches left is:%d\n",sum);
            if(sum==0)
            {
                printf("I'm sorry. You lost!\n");
                exit(0);
            }
            if(sum==1)
                m=1;
            else if(sum<3)
                m=sum-1;
            else
                m=sum%3+1;
            printf("The number of matches that have been moved by the computer is:%d\n",m);
            sum-=m;
            printf("The number of matches left is:%d\n",sum);
            if(sum==0)
            {
                printf("Congratulations!You won!\n");
                exit(0);
            }
        }
        else
        {
            printf("The number you entered is wrong,please re-enter!\n");
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章