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]是正常的,也是允许的。但如果某个评委给每个选手的评分与各选手的最后得分都相差太大,则说明该评委的评分有失水准。可用下面的公式来对各个评委的评分水平进行定量评价:
程序的运行结果示例:
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>
void SortAthletes( int sh[],float sf[],int n);
void SortJudges(float pf[],int ph[] ,int m);
int main()
{
int m,n;
printf("How many Athletes?\n");
scanf("%d",&n);
printf("How many judges?\n");
scanf("%d",&m);
int sh[n],ph[m];
float sf[n],f[n][m],pf[m];
//初始化评委序列号
for(int j=0;j<m;j++){
ph[j]=j+1;
}
//演员初始化,评委打分
printf("Scores of Athletes:\n");
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]);
float max=0,min = 0;
sf[i] = 0;
for(int j=0;j<m;j++){
printf("Judge %d gives score:\n",j+1);
scanf("%f",&f[i][j]);
sf[i]+=f[i][j];
if(j==0){
max = f[i][j];
min = f[i][j];
}else{
if(max < f[i][j]){
max=f[i][j];
}
if(min > f[i][j]){
min=f[i][j];
}
}
}
printf("Delete a maximum score:%.1f\n",max);
printf("Delete a minimum score:%.1f\n",min);
sf[i] = (sf[i]-max-min)/(m-2);
printf("The final score of Athlete %d is %.3f\n",sh[i],sf[i]);
}
//计算评委的分数
for(int i=0;i<m;i++){
float sum =0;
for(int j=0;j<n;j++){
sum+=(f[j][i]-sf[j])*(f[j][i]-sf[j]);
}
pf[i] = 10 - sqrt(sum/n);
}
SortAthletes(sh,sf,n);
SortJudges(pf,ph,m);
return 0;
}
void SortAthletes( int sh[],float sf[],int n){
printf("Order of Athletes:\n");
printf("order\tfinal score\tnumber code\n");
int tempn=0;
float tempc=0;
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
if(sf[i]<sf[j]){
tempn = sh[i];
tempc = sf[i];
sh[i] = sh[j];
sf[i] = sf[j];
sh[j] = tempn;
sf[j] = tempc;
}
}
}
for(int i=0;i<n;i++){
printf("%5d\t%11.3f\t%6d\n",i+1,sf[i],sh[i]);
}
}
void SortJudges(float pf[],int ph[] ,int m){
printf("Order of judges:\n");
printf("order\tfinal score\tnumber code\n");
//评委得分
int tempn=0;
float tempc=0;
for(int i=0;i<m;i++){
for(int j=i;j<m;j++){
if(pf[i]<pf[j]){
tempn = ph[i];
tempc = pf[i];
ph[i] = ph[j];
pf[i] = pf[j];
ph[j] = tempn;
pf[j] = tempc;
}
}
}
for(int i=0;i<m;i++){
printf("%5d\t%11.3f\t%6d\n",i+1,pf[i],ph[i]);
}
printf("Over!Thank you!\n");
}
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!
输入格式:
( 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(void)
{
int n,c;
float score[STU_NUM];
long num[STU_NUM];
char name[STU_NUM][MAX_LEN];
printf("Input student number(n<30):\n");
scanf("%d",&n);
do{
Menu();
scanf("%d",&c);
if(c==1){
ReadScore(num,name,score,n);
}else if(c == 2){
AverSumofScore(score,n);
}else if(c == 3){
SortbyScore(num,name,score,n,Descending);
}else if(c == 4){
SortbyScore(num,name,score,n,Ascending);
}else if(c == 5){
AsSortbyNum(num,name,score,n);
}else if(c == 6){
SortbyName(num,name,score,n);
}else if(c == 7){
SearchbyNum(num,name,score,n);
}else if(c == 8){
SearchbyName(num,name,score,n);
}else if(c == 9){
StatisticAnalysis(score,n);
}else if(c == 10){
PrintScore(num,name,score,n);
}else if(c == 0){
printf("End of program!\n");
}else{
printf("Input error!\n");
}
}while(c!=0);
return 0;
}
int Menu(void){
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");
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)){
if((*compare)(2,1)){
printf("Sort in ascending order by score:\n");
}else{
printf("Sort in descending order by score:\n");
}
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if((*compare)(score[i],score[j])){
SwapFloat(&score[i],&score[j]);
SwapLong(&num[i],&num[j]);
SwapChar(name[i],name[j]);
}
}
}
PrintScore(num,name,score,n);
}
void PrintScore(long num[], char name[][MAX_LEN], float score[], int n){
for(int i=0;i<n;i++){
printf("%ld\t%s\t%.0f\n",num[i],name[i],score[i]);
}
}
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 p ;
p=*x;
*x=*y;
*y=p;
}
void SwapLong(long *x, long *y){
long p ;
p=*x;
*x=*y;
*y=p;
}
void SwapChar(char x[], char y[]){
char c[MAX_LEN];
strcpy(c,x);
strcpy(x,y);
strcpy(y,c);
}
void AsSortbyNum(long num[], char name[][MAX_LEN], float score[], int n){
printf("Sort in ascending order by number:\n");
for(int i=0;i<n-1;i++){
for(int 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]);
}
}
}
PrintScore(num,name,score,n);
}
void SortbyName(long num[], char name[][MAX_LEN], float score[], int n){
printf("Sort in dictionary order by name:\n");
for(int i=0;i<n-1;i++){
for(int 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]);
}
}
}
PrintScore(num,name,score,n);
}
void SearchbyNum(long num[], char name[][MAX_LEN], float score[], int n){
long x;
printf("Input the number you want to search:\n");
scanf("%ld",&x);
int flag = 0;
for(int i=0;i<n;i++){
if(num[i]==x){
printf("%ld\t%s\t%.0f\n",num[i],name[i],score[i]);
flag = 1;
break;
}
}
if(flag == 0){
printf("Not found!\n");
}
}
void SearchbyName(long num[], char name[][MAX_LEN], float score[], int n){
char temp [MAX_LEN];
printf("Input the name you want to search:\n");
scanf("%s",temp);
int flag = 0;
for(int i=0;i<n;i++){
if(strcmp(name[i],temp)==0){
printf("%ld\t%s\t%.0f\n",num[i],name[i],score[i]);
flag = 1;
break;
}
}
if(flag == 0){
printf("Not found!\n");
}
}
void StatisticAnalysis(float score[], int n){
int sum60=0,sum69=0,sum79=0,sum89=0,sum99=0,sum100=0;
for(int i=0;i<n;i++){
if(score[i]<60){
sum60++;
}else if(score[i]>=60 &&score[i]<70){
sum69++;
}else if(score[i]>=70 &&score[i]<80){
sum79++;
}else if(score[i]>=80 &&score[i]<90){
sum89++;
}else if(score[i]>=90 &&score[i]<99){
sum99++;
}else if(score[i]== 100){
sum100++;
}
}
printf("<60\t%d\t%.2f%%\n",sum60,sum60*100.0/n);
printf("%d-%d\t%d\t%.2f%%\n",60,69 ,sum69,sum69*100.0/n);
printf("%d-%d\t%d\t%.2f%%\n",70,79 ,sum79,sum79*100.0/n);
printf("%d-%d\t%d\t%.2f%%\n",80,89 ,sum89,sum89*100.0/n);
printf("%d-%d\t%d\t%.2f%%\n",90,99 ,sum99,sum99*100.0/n);
printf("%d\t%d\t%.2f%%\n",100 ,sum100,sum100*100.0/n);
}
3单词接龙(4分)
题目内容:
阿泰和女友小菲用英语短信玩单词接龙游戏。一人先写一个英文单词,然后另一个人回复一个英文单词,要求回复单词的开头有若干个字母和上一个人所写单词的结尾若干个字母相同,重合部分的长度不限。(如阿泰输入happy,小菲可以回复python,重合部分为py。)现在,小菲刚刚回复了阿泰一个单词,阿泰想知道这个单词与自己发过去的单词的重合部分是什么。他们两人都是喜欢写长单词的英语大神,阿泰觉得用肉眼找重合部分实在是太难了,所以请你编写程序来帮他找出重合部分。
程序运行结果示例1:
happy↙
pythen↙
py
程序运行结果示例2:
sun↙
unknown↙
un
输入格式: “%s%s”
输出格式: “%s\n”
#include<stdio.h>
#include<string.h>
#define N 100
int main(void)
{
char frist[N],second[N],three[N],*p,*p1,*p2,*p3;
scanf("%s%s",frist,second);
p=frist;
p1=second;
while(*p!='\0'){
if(*p == *p1){
three[0]=*p;
int flag=1,i=1;
p2=p;
p3=p1;
while(*p2!='\0'){
if(*p2++ != *p3++ ){
flag = 0;
break;
}else{
three[i]=*p2;
i++;
}
}
if(flag){
three[i] = '\0';
printf("%s\n",three);
break;
}else{
three[0]='\0';
}
}
p++;
}
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
输入提示信息:“Input two FENSHU:\n”
输入格式: “%d/%d,%d/%d”
输出格式:
如果前者大于后者输出提示信息:"%d/%d>%d/%d\n"
如果前者等于后者输出提示信息:"%d/%d=%d/%d\n"
如果前者小于后者输出提示信息:"%d/%d<%d/%d\n"
#include<stdio.h>
#include<string.h>
#define N 100
int main(void)
{
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。
输入格式: 无
输出格式:
输出百万富翁给陌生人的钱: “to Stranger: %.2f yuan\n”
输出陌生人给百万富翁的钱: “to Richman: %.2f yuan\n”
#include<stdio.h>
#include<string.h>
#define N 100
int main(void)
{
double sumS=0,curS=0.005;
for(int i=0;i<30;i++){
curS=2*curS;
sumS+=curS;
}
printf("to Stranger: %.2f yuan\n",sumS);
printf("to Richman: %.2f yuan\n",100000.0*30);
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
输入提示信息: “Input a number:\n”
输入格式: “%d”
输出格式: “sum=%d,count=%d\n”
#include<stdio.h>
#include<string.h>
#define N 100
int main(void)
{
int c,sum=0,count=0;
do{
printf("Input a number:\n");
scanf("%d",&c);
if(c>0){
sum+=c;
count++;
}
}while(c!=0);
printf("sum=%d,count=%d\n",sum,count);
}
7平方根表(4分)
题目内容:
输出100(n2<=100)以内整数的平方根表,n的值要求从键盘输入,并且满足n2<=100 (即n的平方值在100以内)。
程序运行结果示例:
输入提示:“Input n(n<=10):\n”
输入格式: “%d”
输出格式:
输出表头: “%7d”
输出每行的开头数字: “%d”
输出第m行n列中的值:"%7.3f"
#include<stdio.h>
#include<string.h>
#include<math.h>
int main(void)
{
int c;
printf("Input n(n<=10):\n");
scanf("%d",&c);
for(int i=0;i<c;i++){
printf("%7d",i);
}
printf("\n");
for(int i=0;i<c;i++){
printf("%d",i);
for(int j=0;j<c;j++){
printf("%7.3f",sqrt(i*10+j));
}
printf("\n");
}
}
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↙
输入提示信息:“Input a and b:\n”
输入格式: “%d,%d”
输出格式: “Common factor %d is %d\n”
#include<stdio.h>
#include<string.h>
#include<math.h>
int CommonFactors(int a, int b);
int main(void)
{
int a,c;
printf("Input a and b:\n");
scanf("%d,%d",&a,&c);
CommonFactors(a,c);
}
int CommonFactors(int a, int b){
int result = -1,count=0;
int min =a>b?b:a;
if(min<=0){
return result;
}
for(int i=min;i>0;i--){
if(a%i==0 && b%i==0){
printf("Common factor %d is %d\n",++count,i);
result = i;
}
}
return result;
}
923根火柴游戏(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!
游戏开始提示信息:“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(void)
{
int c,a=23,b=0;
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",&c);
if(c > 3 || c > a){
printf("The number you entered is wrong,please re-enter!\n");
}else{
printf("The number of matches you are moving is:%d\n",c);
a -= c;
printf("The number of matches left is:%d\n",a);
if(a > 0){
if(a >= 3){
b=a%3+1;
a -= b;
}else if(a < 3){
b=1;
a -= b;
}
printf("The number of matches that have been moved by the computer is:%d\n",b);
printf("The number of matches left is:%d\n",a);
if(a==0){
printf("Congratulations!You won!\n");
}
}else{
printf("I'm sorry. You lost!\n");
}
}
}while(a > 0);
return 0;
}