题目1069:查找学生信息
时间限制:1 秒
内存限制:32 兆
特殊判题:否
提交:18234
解决:4919
-
题目描述:
-
输入N个学生的信息,然后进行查询。
-
输入:
-
输入的第一行为N,即学生的个数(N<=1000)
接下来的N行包括N个学生的信息,信息格式如下:01 李江 男 2102 刘唐 男 2303 张军 男 1904 王娜 女 19然后输入一个M(M<=10000),接下来会有M行,代表M次查询,每行输入一个学号,格式如下:02030104
-
输出:
-
输出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
-
来源:
- 2003年清华大学计算机研究生机试真题
-
-
#include <cstdio> #include <iostream> #include <cstring> #include <algorithm> using namespace std; struct student{string num;string name;string gender;int age; }stu[1000];bool cmp(struct student stu1,struct student stu2){return stu1.num<stu2.num; }int main(){int n,m;while(scanf("%d",&n)!=EOF){for(int i=0;i<n;i++){cin>>stu[i].num>>stu[i].name>>stu[i].gender>>stu[i].age;}sort(stu,stu+n,cmp);scanf("%d",&m);while(m--){string tag;cin>>tag;int start=0,end=n-1,flag=0;while(end>=start){int mid=(end+start)/2;if(tag>stu[mid].num){start=mid+1;}else if(tag<stu[mid].num){end=mid-1;}else{cout<<stu[mid].num<<" "<<stu[mid].name<<" "<<stu[mid].gender<<" "<<stu[mid].age<<endl;flag=1;break;}} if(flag==0){cout<<"No Answer!"<<endl;}} }return 0; }