九度oj 1069

来源:互联网 发布:断电mysql修复 编辑:程序博客网 时间:2024/06/05 12:50
题目描述:

 输入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年清华大学计算机研究生机试真题

#include<iostream>#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;struct student{       char num[100];       char name[100];       char sex[10];       int age;       bool operator<(const student &a)const       {            return strcmp(num,a.num)<0;            }            }cmp[1000];            int main()            {                int n,ans;                while(cin>>n)                {                             for(int i=0;i<n;i++){                                     cin>>cmp[i].num>>cmp[i].name>>cmp[i].sex>>cmp[i].age;                                     }                                     sort(cmp,cmp+n);                                     int m;                                     cin>>m;                                     while(m--!=0)                                     {                                                   ans=-1;                                                  char x[30];                                                  cin>>x;                                                  int start=0;int end=n-1;                                                  while(end>=start)                                                  {                                                             int mid=(start+end)/2;                                                             int index=strcmp(cmp[mid].num,x);                                                             if(index==0){                                                             ans=mid;                                                             break;    }                                                             else if(index>0)                                                                                                                               end=mid-1;                                                                                                                                    else                                                                  start=mid+1;                                                                  }                                                                                                                                    if(ans==-1){                                                                  cout<<"No Answer!\n";                                                                  }                                                                  else                                                                  printf("%s %s %s %d\n",cmp[ans].num,cmp[ans].name,cmp[ans].sex,cmp[ans].age);                                                                  }                                                                  }                                                                  }


0 0
原创粉丝点击