二分查找
题目描述:
输入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
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
struct Student{
char no[100];
char name[100];
int age;
char sex[10];
bool operator<(const Student & A) const {//重载小于运算符,使其能用sort函数排序
return strcmp(no,A.no)<0;
}
}buf[1000];
int main(){
int n;
cin>>n;
while(n!=EOF){
for(int i=0;i<n;i++){//输入
cin>>buf[i].no>>buf[i].name>>buf[i].sex>>buf[i].age;
}
sort(buf,buf+n);
int t;
cin>>t;
while(t--!=0){//有t组查询
int ans=-1;//目标元素下标,初始化为-1
char x[30];
cin>>x;//输入待查找学号
int top=n-1,base=0;
while(top>=base){//二分查找
int mid=(top+base)/2;
int tmp=strcmp(buf[mid].no,x);//比较中间点和目标点的大小
if(tmp==0){//如果相等,查找完成跳出循环
ans=mid;
break;
}else if(tmp<0){//若小于
base=mid+1;
}else
top=mid-1;
}
if(ans==-1){
cout<<"NO ANSWER"<<endl;
}else
cout<<buf[ans].no<<buf[ans].name<<buf[ans].sex<<buf[ans].age<<endl;
}
}
return 0;
}