问题 B: 各门课的成绩 课后习题11.5
时间限制: 1 Sec 内存限制: 128 MB
提交: 255 解决: 190
[提交][状态][讨论版][命题人:外部导入]
题目描述
有N个学生,每个学生的数据包括学号、姓名、3门课的成绩,从键盘输入N个学生的数据,要求打印出3门课的总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课成绩)
输入
学生数量N占一行每个学生的学号、姓名、三科成绩占一行,空格分开。
输出
各门课的平均成绩 最高分的学生的数据(包括学号、姓名、3门课成绩)
样例输入
2 1 blue 90 80 70 b clan 80 70 60
样例输出
85 75 65 1 blue 90 80 70
代码如下:
#include <iostream>
using namespace std;
struct student{
char xh[20];
char name[20];
double yu,sx,yy;
}stu[200]; //这样是全局变量
int main()
{
int s1=0,s2=0,s3=0,total=0,maxtot=0,maxindex=-1;
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>stu[i].xh>>stu[i].name>>stu[i].yu>>stu[i].sx>>stu[i].yy;
s1 = s1+stu[i].yu;
s2 = s2+stu[i].sx;
s3 = s3+stu[i].yy;
total = stu[i].yu+stu[i].sx+stu[i].yy;
if(total>maxtot){
maxtot=total;
maxindex = i;
}
//scanf()
}
s1= s1/(n);
s2= s2/(n);
s3= s3/(n);
//student stu[200]; 另一种定义方式,到底哪种方式好
cout <<s1<<" "<<s2<<" "<<s3<< endl;
cout <<stu[maxindex].xh<<" "<<stu[maxindex].name<<" "<<stu[maxindex].yu<<" "<<stu[maxindex].sx<<" "<<stu[maxindex].yy<< endl;
return 0;
}