题目链接:http://soj.sysu.edu.cn/1818
题目大意:给出多组学生与成绩的对应数据,要求判断输入的学生的成绩等级,如果有相同名字的,就输出最后一个学生的成绩等级
解题思路:用map存储学生名字与成绩的对应表。此处虽然时间限制是5s,但是仍然不能简单地用两个数组分别存储名字和分数然后再遍历,否则还是会超时。
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
int T, score, n, m, temp;
string name, stu_name;
map<string, int> stu;
map<string, int>::iterator it;
cin >> T;
while (T--)
{
cin >> n >> m;
for (int i = 0; i < n; i++)
{
cin >> name >> score;
//stu.insert(pair<string, int>(name, score)); // 这会保留所有相同名字的学生的成绩,后面查找时会返回第一个找到的学生的成绩
stu[name] = score; // 这会覆盖掉前面相同名字的学生的成绩
}
for (int i = 0; i < m; i++)
{
cin >> stu_name;
it = stu.find(stu_name);
temp = (*it).second;
if (temp > 100 || temp < 0)
cout << "Score is error!" << endl;
else if (temp >= 90)
cout << "A" << endl;
else if (temp >= 80)
cout << "B" << endl;
else if (temp >= 70)
cout << "C" << endl;
else if (temp >= 60)
cout << "D" << endl;
else
cout << "E" << endl;
}
}
return 0;
}