题解 | #查找学生信息#map方法

查找学生信息

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

#include <iostream>
#include <string>
#include <cstdio>
#include <algorithm>
#include <map>

using namespace std;

struct Student { //学生的结构体
    string name;
    string sex;
    int age;
};

int main() {
    int N;//学生的个数
    scanf("%d", &N);
    map<string, Student> myMap;
    //输入学生信息
    for (int i = 0; i < N; i++) {
        char id[30];
        char name[30];
        char sex[30]; //这里如果只有三个位置就会乱码,加大一点就不会了,这也太玄学
        int age;
        scanf("%s %s %s %d", id, name, sex, &age);
        string idStr = id;
        
        Student stu;
        stu.name = name;
        stu.sex = sex;
        stu.age = age;
        myMap[idStr] = stu;
    }
    int M;
    scanf("%d", &M);
    char c[10000];
    for (int i = 0; i < M; i++) {
        scanf("%s", c); //c为学号
        //printf("%s", c);
        string str = c;
        if(myMap.find(str) != myMap.end()){ //找到了
            printf("%s %s %s %d\n", c, myMap[str].name.c_str(),
                     myMap[str].sex.c_str(), myMap[str].age);
        }else{
            printf("No Answer!\n");
        }
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-11 17:10
什么素质,我请问呢,要掉小珍珠了。。。又憋屈又生气
苍蓝星上艾露:给它们能的,一群dinner牛马挥刀向更弱者罢了。我写的开源求职AI co-pilot工具,优化你的简历,找到你匹配的岗位,定制你的简历,并让你做好面试准备https://github.com/weicanie/prisma-ai
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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