题面
问题描述
给定 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
题解
老老实实用结构体做
思路:
记录每个人的学号(id)、姓名(name)、总分(totscore)
排序后输出总分最大者
#include <iostream>
#include <algorithm>
using namespace std;
struct Student{
int totscore;
string id, name;
} a[11];
int n;
bool cmp (Student x, Student y)
{
return x.totscore < y.totscore;
}
int main()
{
cin >> n;
int tmpscore1, tmpscore2, tmpscore3;
for (int i = 1; i <= n; i++)
{
cin >> a[i].id >> a[i].name >> tmpscore1 >> tmpscore2 >> tmpscore3;
a[i].totscore += tmpscore1 + tmpscore2 + tmpscore3;
}
sort(a + 1, a + n + 1, cmp);
cout << a[n].name << ' ' << a[n].id << ' ' << a[n].totscore;
return 0;
}
date:20231231