请用C++编写一个程序;在某次考试中,五(2)班有学生n名,已知每位学生的姓名及他的语文成绩,求获得最高分数的学生的姓名。
时间: 2024-04-15 11:30:16 浏览: 31
下面是使用 C++ 编写的程序,用于找出五(2)班中获得最高分数的学生姓名:
```cpp
#include <iostream>
#include <string>
#include <vector>
// 学生类
class Student {
public:
std::string name;
int score;
Student(std::string n, int s) {
name = n;
score = s;
}
};
int main() {
int n; // 学生人数
std::cout << "请输入学生人数: ";
std::cin >> n;
std::vector<Student> students; // 存储学生信息的向量
// 输入学生信息
for (int i = 0; i < n; i++) {
std::string name;
int score;
std::cout << "请输入第 " << i + 1 << " 位学生的姓名: ";
std::cin >> name;
std::cout << "请输入第 " << i + 1 << " 位学生的语文成绩: ";
std::cin >> score;
students.push_back(Student(name, score)); // 将学生信息添加到向量中
}
// 找出最高分数的学生
int maxScore = students[0].score;
std::string maxScoreStudent = students[0].name;
for (int i = 1; i < n; i++) {
if (students[i].score > maxScore) {
maxScore = students[i].score;
maxScoreStudent = students[i].name;
}
}
// 输出最高分数的学生姓名
std::cout << "获得最高分数的学生是: " << maxScoreStudent << std::endl;
return 0;
}
```
这个程序会依次询问学生的姓名和语文成绩,然后找出获得最高分数的学生的姓名并输出。请根据实际情况进行编译和运行。