pta6-2 结构体查找——根据姓名查询成绩
时间: 2023-11-11 09:04:31 浏览: 109
好的,我了解了。关于您的问题,我可以为您提供以下代码:
```c++
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int score;
};
int main() {
int n;
cin >> n;
Student* arr = new Student[n];
for(int i = 0; i < n; i++) {
cin >> arr[i].name >> arr[i].score;
}
string query;
cin >> query;
for(int i = 0; i < n; i++) {
if(arr[i].name == query) {
cout << arr[i].score << endl;
break;
}
else if(i == n-1) {
cout << "Not Found" << endl;
}
}
return 0;
}
```
这个程序定义了一个 `Student` 结构体,包含学生姓名和成绩。用户输入学生的数量和每个学生的姓名和成绩,然后根据输入的姓名查询对应的成绩。如果找到了对应的学生,程序会输出对应的成绩;如果找不到,程序会输出 "Not Found"。
希望这个程序对您有所帮助。如果您有任何其他问题,可以继续问我。