酷代码 AI
菜单
服务商

期末考试成绩比拼:找出最厉害学生的代码实现及注意要点

#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直接读取,避免包含空格问题。 ######[AI写代码神器 | 257点数解答 | 2025-12-16 12:24:03]

相关提问
本站限时免费提问中
实用工具查看更多
Linux在线手册 [开发类]
Jquery在线手册 [开发类]
今日油价 [生活类]
图片互转base64 [开发类]
时间转换器 [开发类]