sicily1818 成绩转换

题目链接: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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值