Topic 1051: [Introduction to Programming] Score Statistics of Structure 2

Question description

There are N students. Each student's data includes student number, name, and grades of 3 courses . Enter the data of N students from the keyboard and request to print out the total average grade of the 3 courses and the data of the student with the highest score ( Including student number, name, and grades of 3 courses)

Input format

The number of students N occupies one line. Each student's student number, name, and three subject scores occupy one line and are separated by spaces.

Output format

Data on students with the highest average scores in each course (including student number, name, and scores in 3 courses)

Sample input

2
1 blue 90 80 70
b clan 80 70 60

Sample output

85 75 65
1 blue 90 80 70

The idea of ​​this question is similar to the previous question. It also counts student scores , but adds the average score of each subject and the highest statistical score (what I understand is the total highest score of the three subjects). Following the input function from the previous question (calculating the total score of each subject and the total score of each person's three subjects), the structure is written in a different way, and a structure array is still defined . When outputting, compare each person's total score in the three subjects and return the serial number . Finally, just output the data according to the format!

#include<iostream>
#include<string>
using namespace std;

typedef struct {
	string num;  //学号
	string name; //成绩
	int s1;      //科目一 大战英语四六八级
	int s2;      //科目二 考研8511系
	int s3;      //科目三 灵车飘移
}Student;  //结构体名称 

Student stu[100];  //结构体数组

int sum1=0, sum2=0, sum3=0;  //每一科的总分,求平均分
int grades[100] = {0};

int input() {
	int n;   //多少名学生
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> stu[i].num;  //输入学号
		cin >> stu[i].name;
		cin >> stu[i].s1;
		   
		cin >> stu[i].s2;
	
		cin >> stu[i].s3;

		sum1 += stu[i].s1;  //科目一总分
		sum2 += stu[i].s2;
        sum3 += stu[i].s3;
		grades[i] = stu[i].s1 + stu[i].s2 + stu[i].s3;//个人总成绩
	}
	return n;
}
//秘制输出函数 附带 
void print2(int cnt) {
	//求各科平均分
	sum1 = sum1 / cnt;
	sum2 = sum2 / cnt;
	sum3 = sum3 / cnt;//科目三平均分
	/*比较出总分最高分 */
	int Max = grades[0]; 
	int c = 0;  //最高成绩的序号

	for (int i = 1; i < cnt; i++) {
		int temp = Max;
		//三目赋值运算符 是就返回"?"后的,不是就返回": "后的
		Max = Max > grades[i] ? Max : grades[i];
		if (Max != temp) {
		   //说明换了
			c = i;  //返回序号
		}
	}
	//开始输出 
	cout << sum1 << " " << sum2 << " " << sum3 << endl;// 三科平均分
	cout << stu[c].num << " " << stu[c].name << " " << stu[c].s1 << " " << stu[c].s2 << " " << stu[c].s3 << endl;


}

int main() {
	int number = input();

	print2(number);


	return 0;
}

Guess you like

Origin blog.csdn.net/qq_63999224/article/details/132843428