九度oj 题目1069:查找学生信息--字典

链接

http://ac.jobdu.com/problem.php?pid=1069


题目描述:

 输入N个学生的信息,然后进行查询。

输入:

 输入的第一行为N,即学生的个数(N<=1000)

接下来的N行包括N个学生的信息,信息格式如下:
01 李江 男 21
02 刘唐 男 23
03 张军 男 19
04 王娜 女 19
然后输入一个M(M<=10000),接下来会有M行,代表M次查询,每行输入一个学号,格式如下:
02
03
01
04
输出:

 输出M行,每行包括一个对应于查询的学生的信息。

如果没有对应的学生信息,则输出“No Answer!”
样例输入:
4
01 李江 男 21
02 刘唐 男 23
03 张军 男 19
04 王娜 女 19
5
02
03
01
04
03
样例输出:
02 刘唐 男 23
03 张军 男 19
01 李江 男 21
04 王娜 女 19
03 张军 男 19

思路

用字典的数据结构,将学号存储称字符串,用作索引


c++ map stl:

数据的清空与判空
清空map中的数据可以用clear()函数,判定map中是否有数据可以用empty()函数,它返回true则说明是空map


ac代码

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
#include <map>
using namespace std;
struct student
{
    char name[100];
    char sex[100];
    int age;
};
int main()
{
    map<string,student> a;
    int T;
    string num;
    while(cin >> T){
    for(int i=0;i<T;i++)
    {
        student u;
        cin >>num >> u.name >> u.sex >> u.age;
        a[num] = u;
    }
    cin >> T;
    for(int i=0;i<T;i++)
    {
        string temp;
        cin >> temp;
        if(a.find(temp) != a.end())
            cout << temp << ' ' << a[temp].name << ' ' << a[temp].sex << ' ' << a[temp].age << endl;
        else
            printf("No Answer!\n");
    }
    }
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值