-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9SearchStudentInfo1069.cpp
59 lines (56 loc) · 1.21 KB
/
9SearchStudentInfo1069.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
class Stu
{
public:
char id[100];
string name;
string sex;
int age;
bool operator <(const Stu &s)const
{
return strcmp(id,s.id)<0;
}
} stu[1000];
int BinarySearch(Stu arr[],int left,int right,char sear[])
{
while(left<=right)
{
int mid=(left+right)/2;
if(strcmp(arr[mid].id,sear)==0)
return mid;
else if(strcmp(arr[mid].id,sear)>0)
right=mid-1;
else
left=mid+1;
}
if(left>right)
return -1;
}
int main()
{
int n,m;//n student's info; search for m times
while(cin>>n&&n<=1000)
{
for(int i=0; i<n; i++)
{
cin>>stu[i].id>>stu[i].name>>stu[i].sex>>stu[i].age;
}
sort(stu,stu+n);
cin>>m;
char sea[30];
for(int j=0; j<m; j++)
{
cin>>sea;
int index=BinarySearch(stu,0,n-1,sea);
if(index!=-1)
cout<<stu[index].id<<" "<<stu[index].name<<" "<<stu[index].sex<<" "<<stu[index].age<<endl;
else
cout<<"No Answer!"<<endl;
}
return 0;
}
}