给定N个学生的基本信息,包括学号(由5个数字组成的字符串)、姓名(长度小于10的不包含空白字符的非空字符串)和3门课程的成绩([0,100]区间内的整数),要求输出总分最高学生的姓名、学号和总分。
输入在一行中给出正整数N(≤10)。随后N行,每行给出一位学生的信息,格式为“学号 姓名 成绩1 成绩2 成绩3”,中间以空格分隔。
在一行中输出总分最高学生的姓名、学号和总分,间隔一个空格。题目保证这样的学生是唯一的。
5
00001 huanglan 78 83 75
00002 wanghai 76 80 77
00003 shenqiang 87 83 76
10001 zhangfeng 92 88 78
21987 zhangmeng 80 82 75
zhangfeng 10001 258
错误答案一:
#include
struct stu
{
int num;
char name[11];//这里最好大于题目的要求
int sco1;
int sco2;
int sco3;
int all;
};
#define MAX 11
int main()
{
int n = 0;
scanf("%d", &n);
struct stu st[MAX];
int m = 0;
int max = 0;
for (int i = 0;i < n;i++)
{
scanf("%d %s %d %d %d", &st[i].num, st[i].name, &st[i].sco1, &st[i].sco2, &st[i].sco3);
st[i].all = st[i].sco1 + st[i].sco2 + st[i].sco3;
if (i == 0)
{
max = st[i].all;
}
else
{
if (max < st[i].all)
{
max = st[i].all;
m = i;
}
}
}
printf("%s %d %d", st[m].name, st[m].num, st[m].all);
return 0;
}
//错误原因:发现输出学号时,需要用%05d,而不是%d或者%5d。原题的要求是:给定N个学生的基本信息,包括学号(由5个数字组成的字符串)、姓名(长度小于10的不包含空白字符的非空字符串)和3门课程的成绩([0,100]区间内的整数),要求输出总分最高学生的姓名、学号和总分。
//解释:因为题目上对学号有要求,但是数字的输入又没有办法来限制,所以只好在输出上下功夫了
正确答案:
#include
struct stu
{
int num;
char name[11];//这里最好大于题目的要求
int sco1;
int sco2;
int sco3;
int all;
};
#define MAX 11
int main()
{
int n = 0;
scanf("%d", &n);
struct stu st[MAX];
int m = 0;
int max = 0;
for (int i = 0;i < n;i++)
{
scanf("%d %s %d %d %d", &st[i].num, st[i].name, &st[i].sco1, &st[i].sco2, &st[i].sco3);
st[i].all = st[i].sco1 + st[i].sco2 + st[i].sco3;
if (i == 0)
{
max = st[i].all;
}
else
{
if (max < st[i].all)
{
max = st[i].all;
m = i;
}
}
}
printf("%s %05d %d", st[m].name, st[m].num, st[m].all);
return 0;
}
其它的正确答案:
这种我也不明白为什么对
#include
//
//struct student
//{
// int id[5];
// char name[10];
// int a, b, c;
// int sum;
//}stu[10];
//int main()
//{
// int i, j, N, max;
//
// scanf("%d", &N);
// for (i = 0; i < N; i++)
// {
// scanf("%s %s %d %d %d", &stu[i].id, stu[i].name, &stu[i].a, &stu[i].b, &stu[i].c);
// stu[i].sum = stu[i].a + stu[i].b + stu[i].c;
// }
// for (j = i = 0; i < N; i++)
// {
// if (stu[j].sum < stu[i].sum)
// {
// j = i;
// }
// }
// printf("%s %s %d", stu[j].name, stu[j].id, stu[j].sum);
//}
struct stu
//{
// char num[6];//最好比题目的要求大,不然\0读不到
// char name[10];
// int sco1;
// int sco2;
// int sco3;
// int all;
//};
//#define MAX 10
//int main()
//{
// int n = 0;
// scanf("%d", &n);
// struct stu st[MAX];
// int m = 0;
// int max = 0;
// for (int i = 0;i < n;i++)
// {
// scanf("%s %s %d %d %d", st[i].num, st[i].name, &st[i].sco1, &st[i].sco2, &st[i].sco3);
// st[i].all = st[i].sco1 + st[i].sco2 + st[i].sco3;
// if (i == 0)
// {
// max = st[i].all;
// }
// else
// {
// if (max < st[i].all)
// {
// max = st[i].all;
// m = i;
// }
// }
// }
// printf("%s %s %d", st[m].name, st[m].num, st[m].all);
// return 0;
//}