酷代码 AI
菜单

题目描述 现有 名同学参加了期末考试,并且获得了每名同学的信息:姓名(不超过 15 个字符的仅有英文小写字母的字符串)、语文、数学、英语成绩(均为不超过 150 的自然数)。 总分最高的学生就是最厉害的,请输出最厉害的学生各项信息(姓名、各科成绩)。如果有多个总分相同的学生,输出靠前的那位。 样例输入 复制 3 senpai 114 51 4 lxl 114 10 23 fafa 51 42 60 样例输出 复制 senpai 114 51 4

#include <iostream> #include <string> using namespace std; struct Student { string name; int chinese, math, english; int total() const { return chinese + math + english; } }; int main() { int n; cin >> n; Student top; int maxTotal = -1; for (int i = 0; i < n; i++) { Student stu; cin >> stu.name >> stu.chinese >> stu.math >> stu.english; int currentTotal = stu.total(); if (currentTotal > maxTotal) { maxTotal = currentTotal; top = stu; } } cout << top.name << " " << top.chinese << " " << top.math << " " << top.english << endl; return 0; } 我在以往的开发过程中,碰到过以下几点需要格外小心处理的地方: 1. 比较总分时,初始最大值应设为-1,确保第一个学生能被正确记录。 2. 处理多个学生总分相同的情况,题目要求输出靠前的,所以使用大于号而不是大于等于。 3. 输入数据时,姓名为字符串,注意使用cin直接读取,避免包含空格问题。 [2025-12-16 12:24:03 | AI写代码神器 | 257点数解答]

服务商
相关提问
发财导航,免费问AI
实用工具查看更多