c++找出总分最高的学生
时间: 2024-10-08 15:04:14 浏览: 50
找出成绩最高者
5星 · 资源好评率100%
在 C++ 中,如果你有一个包含学生成绩的学生信息结构体或者类,你可以通过定义一个函数来找出总分最高的学生。假设你有如下的简单结构体 `Student`:
```cpp
struct Student {
string name;
int scores[3]; // 假设这里有三门课程的成绩
};
```
你可以遍历所有学生并计算他们的总分,同时保存最高分和对应的姓名:
```cpp
int findHighestScore(Student students[], int count) {
int highestScore = INT_MIN; // 初始化最高分为最小整数
string studentWithHighestScore;
for (int i = 0; i < count; ++i) {
int total = students[i].scores[0] + students[i].scores[1] + students[i].scores[2];
if (total > highestScore) {
highestScore = total;
studentWith HighestScore = students[i].name;
}
}
return highestScore, studentWithHighestScore;
}
```
然后你可以像这样使用这个函数:
```cpp
int main() {
Student students[] = {{"Tom", {85, 90, 92}}, {"Jerry", {88, 95, 97}}, {"Spike", {75, 80, 88}}};
int numStudents = sizeof(students) / sizeof(students[0]);
int maxScore, topStudent;
tie(maxScore, topStudent) = findHighestScore(students, numStudents);
cout << "The student with the highest score is " << topStudent << " with a total of " << maxScore << "." << endl;
return 0;
}
```
阅读全文