C语言程序设计精髓第十二周编程题

练兵区

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]是正常的,也是允许的。但如果某个评委给每个选手的评分与各选手的最后得分都相差太大,则说明该评委的评分有失水准。可用下面的公式来对各个评委的评分水平进行定量评价:
在这里插入图片描述

#include 
#include
#define N 20
int MaxPos(float f[][N], int x, int judge);
int MinPos(float f[][N], int x, int judge);
void Arrange(float score[], int n, int number[])
{
    int i, j;
    float temp;
    for(i=0;i<n-1;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(score[i]<score[j])
            {
                temp = score[i];
                score[i]=score[j];
                score[j]=temp;
                temp = number[i];
                number[i]=number[j];
                number[j]=temp;
            }
        }
    }
}
int main()
{
    int athlete, judge, sh[N];//存放选手编号
    int ph[N]; //评委编号
    float f[N][N]; //评委给选手的评分
    float sf[N]; //存放最后得分
    float pf[N]; //评委评分水准分数
    float sum;
    float x; //计算评委分数时用到的变量
    int maxpos, minpos;
    int i, j;
    printf("How many Athletes?\n");
    scanf("%d", &athlete);
    printf("How many judges?\n");
    scanf("%d", &judge);
    printf("Scores of Athletes:\n");
    for(i=0;i<athlete;i++)
    {
        printf("Athlete %d is playing.\n", i+1);
        printf("Please enter his number code:\n");
        scanf("%d", &sh[i]);
        for(j=0;j<judge;j++)
        {
            printf("Judge %d gives score:\n", j+1);
            scanf("%f", &f[i][j]);
        }
        maxpos =MaxPos(f, i, judge);
        minpos=MinPos(f, i, judge);
        printf("Delete a maximum score:%.1f\n", f[i][maxpos]);
        printf("Delete a minimum score:%.1f\n", f[i][minpos]);
        sum = 0;
        for(j=0;j<judge;j++)
        {
            sum += f[i][j];
        }
        sum = sum - f[i][minpos] - f[i][maxpos];
        sf[i] = sum/(judge-2);
        printf("The final score of Athlete %d is %.3f\n", sh[i], sf[i]);
    }
    for (j = 0; j < judge; j++) //计算评委分数,由于Arrange会交换sf位置,这一步一定要在Arrange前
    {
        x = 0;
        for (i = 0; i < athlete; i++)
        {
            x = x + pow(f[i][j] - sf[i], 2);
        }
        x = x / athlete;
        pf[j] = 10 - sqrt(x);
    }
    printf("Order of Athletes:\n");
    Arrange(sf, athlete, sh);
    printf("order\tfinal score\tnumber code\n");
    for(i=0;i<athlete;i++)
        printf("%5d\t%11.3f\t%6d\n", i+1, sf[i], sh[i]);
    printf("Order of judges:\n");
    for(i=0;i<judge;i++)
        ph[i]=i+1;
    Arrange(pf, judge, ph);
    printf("order\tfinal score\tnumber code\n");
    for(i=0;i<judge;i++)
        printf("%5d\t%11.3f\t%6d\n", i+1, pf[i], ph[i]);
    printf("Over!Thank you!\n");
    return 0;
    
}

int MaxPos(float f[][N], int x, int judge)
{
    int maxpos = 0, i;
    for(i=0;i<judge;i++)
    {
        if(f[x][i]>f[x][maxpos])
            maxpos=i;
    }
    return maxpos;
}

int MinPos(float f[][N], int x, int judge)
{
    int minpos = 0, i;
    for(i=0;i<judge;i++)
    {
        if(f[x][i]<f[x][minpos])
            minpos=i;
    }
    return minpos;
}

2. 学生成绩管理系统V3.0(4分)

题目内容:

某班有最多不超过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!

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

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

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;
    float average = 0;
    int i;
    for(i=0;i<n;i++)
    {
        sum+=score[i];
    }
    average = sum/n;
    printf("sum=%.0f,aver=%.2f\n", sum, average);
}

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])==1)
            {
                SwapFloat(&score[i], &score[j]);
                SwapLong(&num[i],&num[j]);
                SwapChar(name[i], name[j]);
            }
        }
    }
    for(i=0;i<n;i++)
        printf("%ld\t%s\t%.0f\n", num[i], name[i], score[i]);
    
}

int   Ascending(float a, float b)
{
    if(a>b)
        return 1;
    return 0;
}
int   Descending(float a, float b)
{
    if(a<b)
        return 1;
    return 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 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]);
                SwapLong(&num[i],&num[j]);
                SwapChar(name[i], name[j]);
            }
        }
    }
    for(i=0;i<n;i++)
        printf("%ld\t%s\t%.0f\n", num[i], name[i], score[i]);
}

void  SortbyName(long num[], char name[][MAX_LEN], float score[], int n)
{
    int i, j;
    printf("Sort in dictionary order by name:\n");
    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]);
                SwapLong(&num[i],&num[j]);
                SwapChar(name[i], name[j]);
            }
        }
    }
    for(i=0;i<n;i++)
        printf("%ld\t%s\t%.0f\n", num[i], name[i], score[i]);
}

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

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

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

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]);
    }
}

3. 单词接龙(4分)

题目内容:

阿泰和女友小菲用英语短信玩单词接龙游戏。一人先写一个英文单词,然后另一个人回复一个英文单词,要求回复单词的开头有若干个字母和上一个人所写单词的结尾若干个字母相同,重合部分的长度不限。(如阿泰输入happy,小菲可以回复python,重合部分为py。)现在,小菲刚刚回复了阿泰一个单词,阿泰想知道这个单词与自己发过去的单词的重合部分是什么。他们两人都是喜欢写长单词的英语大神,阿泰觉得用肉眼找重合部分实在是太难了,所以请你编写程序来帮他找出重合部分。

程序运行结果示例1:

happy↙

pythen↙

py

程序运行结果示例2:

sun↙

unknown↙

un

#include 
#include
#include
#define  N 20
int main()
{
    char a[N], b[N], c[N];
    long lena, lenb;
    long i, j=0;
    scanf("%s%s", a, b);
    lena = strlen(a);
    lenb = strlen(b);
    for(i=0;i<lena;i++)
    {
        if(a[i]==b[j])
        {
            c[j]=b[j];
            for(j=1;j<lenb;j++)
            {
                if(a[i+j]==b[j])
                    c[j]=b[j];
                else
                    break;
            }
            
        }
    }
    c[j+1]='\0';
    printf("%s\n", c);
    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");

程序运行结果示例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

#include 
#include
#include
#define  N 20
int main()
{
    int a, b, c, d;
    printf("Input two FENSHU:\n");
    scanf( "%d/%d,%d/%d", &a, &b, &c, &d);
    if(a*d<c*b)
        printf("%d/%d<%d/%d\n", a, b, c, d);
    else if(a*d==c*b)
        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。

输入格式: 无

输出格式:

输出百万富翁给陌生人的钱: “to Stranger: %.2f yuan\n”

输出陌生人给百万富翁的钱: “to Richman: %.2f yuan\n”

#include 
#include
#include
#define  N 20

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

6. 用计数控制的循环实现正数累加求和(4分)

题目内容:

输入一些整数,编程计算并输出其中所有正数的和,输入负数时不累加,继续输入下一个数。输入零时,表示输入数据结束。要求最后统计出累加的项数。

程序运行结果示例:

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

#include 
#include
#include
#define  N 20

int main()
{
    int x, i, sum=0, count=0;
    for(i=0;;i++)
    {
        printf( "Input a number:\n");
        scanf("%d", &x);
        if(x<0)
            continue;
        else if(x>0)
        {
            sum += x;
            count++;
        }
        else
            break;
    }
    printf( "sum=%d,count=%d\n", sum, count);
}

7. 平方根表(4分)

题目内容:

输出100(n2<=100)以内整数的平方根表,n的值要求从键盘输入,并且满足n2<=100 (即n的平方值在100以内)。

程序运行结果示例:
C语言程序设计精髓第十二周编程题_第1张图片

#include 
#include
#include
#define  N 20

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


8. 最大公约数(4分)

题目内容:

按照如下函数原型编写子函数计算正整数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↙

#include 
#include
#include
#define  N 20
int divisor, count = 0;
int CommonFactors(int a, int b);
int main()
{
    int a, b, ret;
    printf("Input a and b:\n");
    scanf("%d,%d", &a, &b);
    do{
        ret = CommonFactors(a, b);
    }while(ret!=-1);
    return 0;
}

 int CommonFactors(int a, int b)
{
    int i;
    if(count==0)
    {
        if(a>b)
            divisor = b;
        else
            divisor = a;
    }
    if(a<0||b<0)
        return -1;
    for(i=divisor;i>0;i--)
    {
        if(a%i==0&&b%i==0)
        {
            count ++;
            divisor = i-1;
            printf("Common factor %d is %d\n", count, i);
            return i;
        }
    }
    return -1;
}

9. 23根火柴游戏(4分)

题目内容:

请编写一个简单的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!

#include 
#include
#include
#define  N 20

int main()
{
    int n, m, match = 23, flag=0;  //代表下一步操作的人,若为0,人移动火柴;若为1,则计算机移动。
    printf("Game start!\n");
    printf( "Note: the maximum number is 3\n");
    do{
        printf("Please enter the number of matches you are moving:\n");
        scanf("%d", &n);
        if(n>3||n<1)
        {
            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);
            match -= n;
            printf("The number of matches left is:%d\n", match);
            flag = 1;
        }
        if(match == 0)
            break;
        if(match < 3)
            m = 1;
        else
            m = match % 3 + 1;
        printf("The number of matches that have been moved by the computer is:%d\n", m);
        match -= m;
        printf("The number of matches left is:%d\n", match);
        flag = 0;
    }while(match!=0);
    if (flag == 0)
        printf("Congratulations!You won!\n");
    else
        printf("I'm sorry. You lost!\n");
    return 0;
}



在线测试

1. 计算时间差V2.0(4分)

题目内容:

用结构体定义时钟类型,编程从键盘任意输入两个时间(例如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

#include 
#include
#include
#define  N 20
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);
    if(t1.hour>t2.hour) //使得函数中t1大于t2
        t3 = CalculateTime(t1, t2);
    else if(t2.hour>t1.hour)
        t3 = CalculateTime(t2, t1);
    else
    {
        if(t1.minute>t2.minute)
            t3 = CalculateTime(t1, t2);
        else
            t3 = CalculateTime(t2, t1);
    }
    printf("%dhour,%dminute\n", t3.hour, t3.minute);
    return 0;
}

CLOCK CalculateTime(CLOCK t1, CLOCK t2)
{
    CLOCK t3;
    t3.minute = t1.minute - t2.minute;
    if(t3.minute<0)
    {
        t3.minute+=60;
        t3.hour = t1.hour - t2.hour -1;
    }
    else
        t3.hour = t1.hour - t2.hour;
    return t3;
}


2. 奖学金发放(4分)

题目内容:

某校的惯例是在每学期的期末考试之后发放奖学金。发放的奖学金共有五种,每项奖学金获取的条件分别如下:

  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

#include 
#include
#include
#define  N 20
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, max;
    WIN student[N];
    printf("Input n:"); //输入学生人数
    scanf("%d", &n);
    for(i=0;i<n;i++)
    {
        printf("Input name:");
        scanf("%s", student[i].name);
        printf("Input final score:");
        scanf("%d", &student[i].finalScore);
        printf("Input class score:");
        scanf("%d", &student[i].classScore);
        printf("Class cadre or not?(Y/N):");
        scanf(" %c", &student[i].work);
        printf("Students from the West or not?(Y/N):");
        scanf(" %c", &student[i].west);
        printf("Input the number of published papers:");
        scanf( "%d", &student[i].paper);
        Addup(student, i);//计算该学生总奖学金数
        printf( "name:%s,scholarship:%d\n", student[i].name, student[i].scholarship);
    }
    max = FindMax(student, n); //返回奖学金最高的学生数组下标值
    printf("%s get the highest scholarship %d\n", student[max].name, student[max].scholarship);
    return 0;
}

void Addup(WIN stu[], int n)
{
    stu[n].scholarship = 0;
    if(stu[n].finalScore>80&&stu[n].paper>=1) //院士奖学金
        stu[n].scholarship+=8000;
    if(stu[n].finalScore>85 && stu[n].classScore>80) //五四奖学金
        stu[n].scholarship += 4000;
    if(stu[n].finalScore>90) //成绩优秀奖
        stu[n].scholarship +=2000;
    if(stu[n].finalScore>85 && stu[n].west == 'Y')//西部奖学金
        stu[n].scholarship +=1000;
    if(stu[n].classScore>80 && stu[n].work == 'Y') //班级贡献奖
        stu[n].scholarship +=850;
}

int FindMax(WIN student[], int n)
{
    int max, i;
    max = 0;
    for(i=1;i<n;i++)
    {
        if(student[max].scholarship<student[i].scholarship)
            max = i;
    }
    return max;
}

3. 评选最牛群主v1.0(4分)

题目内容:
现在要评选最牛群主,已知有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

#include 
#include
#include
#define  N 1000

int main()
{
    int n, i;
    int tom=0, jack=0, rose=0; //统计票数
    char candidate1[5] = "tom", candidate2[5] = "jack", candidate3[5] = "rose";
    char voting[N][5];
    printf("Input the number of electorates:");
    scanf("%d", &n);
    for(i=0;i<n;i++)
    {
        printf( "Input vote %d:", i+1);
        scanf("%s", voting[i]);
    }
    for(i=0;i<n;i++)
    {
        if(strcmp(voting[i],candidate1)==0)
            tom++;
        else if(strcmp(voting[i],candidate2)==0)
            jack++;
        else if(strcmp(voting[i],candidate3)==0)
            rose++;
    }
    printf("Election results:\n");
    printf("%s:%d\n", candidate1, tom);
    printf("%s:%d\n", candidate2, jack);
    printf("%s:%d\n", candidate3, rose);
    if(tom>jack && tom>rose)
        printf("%s wins\n", candidate1);
    else if(jack>rose && jack>tom)
        printf("%s wins\n", candidate2);
    else
        printf("%s wins\n", candidate3);
    return 0;
}


4. 星期判断(4分)

**题目内容:**请输入星期几的第一个字母(不区分大小写)来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母(小写),否则输出“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

#include 
#include
#include
#define  N 1000

int main()
{
    char c1, c2;
    printf("please input the first letter of someday:\n");
    scanf(" %c", &c1);
    if(c1 == 'T' || c1=='t'|| c1 == 'S'||c1=='s')
    {
        printf("please input second letter:\n");
        scanf(" %c", &c2);
    }
    switch(c1)
    {
        case 'M':
        case 'm': printf("monday\n"); break;
        case 'W':
        case 'w': printf("wednesday\n"); break;
        case 'F':
        case 'f': printf("friday\n"); break;
        case 'S':
        case 's':
        {
            if(c2 == 'a')
                printf("saturday\n");
            else if(c2 == 'u')
                printf("sunday\n");
            else
                printf("data error\n");
            break;
        }
        case 'T':
        case 't':
        {
            if(c2 == 'h')
                printf("thursday\n");
            else if(c2 == 'u')
                printf("tuesday\n");
            else
                printf("data error\n");
            break;
        }
        default: printf("data error\n");
            break;
    }
}


你可能感兴趣的:(C语言)