题目1069:查找学生信息

来源:互联网 发布:提高阅读速度软件 编辑:程序博客网 时间:2024/05/17 23:46

时间限制:1 秒

内存限制:32 兆

特殊判题:

提交:9863

解决:2625

题目描述:

 输入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!”
样例输入:
401 李江 男 2102 刘唐 男 2303 张军 男 1904 王娜 女 1950203010403
样例输出:
02 刘唐 男 2303 张军 男 1901 李江 男 2104 王娜 女 1903 张军 男 19
来源:
2003年清华大学计算机研究生机试真题

【分析】对于有唯一标志可以想到用map K-V对来解决会非常简单。并且自定义输出使代码更简洁。


#include <iostream>#include <stdio.h>#include <map>using namespace std;class Student{    public:        string number;        string name;        string sex;        int age;        //自定义输出全部信息        void outPut()        {            cout<<number<<' '<<name<<' '<<sex<<' '<<age<<endl;        }};int main(){    //freopen("d:\\in.txt","r",stdin);    int n,count;    while(cin>>n)    {        map<string,Student> mss;        string num;        for(int i=0;i<n;i++)        {            Student student;            cin>>student.number>>student.name>>student.sex>>student.age;             //保存学生信息到map中            mss[student.number] = student;        }        cin>>count;        for(int i=0;i<count;i++)        {            cin>>num;            //如果没找到信息返回map.end()            if(mss.find(num)==mss.end())                cout<<"No Answer!"<<endl;            else                mss[num].outPut();        }    }    return 0;}


0 0
原创粉丝点击