题解 | #查找学生信息#

查找学生信息

https://www.nowcoder.com/practice/fe8bff0750c8448081759f3ee0d86bb4

#include <iostream>
#include "unordered_map"
using namespace std;
struct student {
    string name;
    string sex;
    int age;
    
    
};
int main() {
    int N;
    while (cin >> N) { // 注意 while 处理多个 case
        // cout << a + b << endl;
        unordered_map <int, student> myMap;
        while (N--) {
            int num;
            string name;
            string sex;
            int age;
            cin >> num >> name >> sex >> age;
            // myMap.insert(pair<int,student>(num,student(name, sex, age))) ;
            student temp;
            temp.name=name;
            temp.age=age;
            temp.sex=sex;
            myMap[num]=temp;//使用下标访问时结构体必须是可默认构造的
        }
        int M;
        cin >> M;
        while (M--) {
            int num;
            cin >> num;
            if (myMap.find(num) == myMap.end())
                cout << "No Answer!" << endl;
            else {
                student result = myMap.find(num)->second;
                cout << num << ' ' << result.name << ' ' << result.sex << ' ' << result.age <<
                     endl;
            }

        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

测试糕手手:社会第一课,随便吹牛逼,直接说四个月,别老实。老实人只会被欺负
点赞 评论 收藏
分享
那一天的Java_J...:他本来公司就是做这个的,不就是正常的游戏客户端和服务器开发,软硬件联动,有啥恶心不恶心的,提前告诉你就是怕你接受不了,接受不了就没必要再往后走流程浪费时间,虽然这公司是一坨。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务