#include <stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
struct student{
char id[20];a
char spe[20];
bool operator <(const student &E)const{
return strcmp(id,E.id)<0;
}
}buf[1000];
int main(){
int N,m;
while(scanf("%d",&N)!=EOF){
for(int i=0;i<N;i++){
scanf("%s",&buf[i].id);
// getchar();
gets(buf[i].spe);
cout<<buf[i].id<<endl;
cout<<buf[i].spe<<endl;
}
sort(buf,buf+N);
cin>>m;
while(m--){
char no[20];
cin>>no;
int mid;
for(int i=0,j=N-1;i<=j;){
mid=(i+j)/2;
if(strcmp(buf[mid].id,no)==0){
break;
}
else if(strcmp(buf[mid].id,no)<0){
i=mid+1;
}
else{
j=mid-1;
}
}
if(strcmp(buf[mid].id,no)!=0){
cout<<"No Answer!\n"<<endl;
}
else{
cout<<buf[mid].id<<buf[mid].spe<<endl;
}
}
}
return 0;
}
记着:如果你首先用scanf的形式输入并以回车结束,回车符肯定被留在了缓冲区中,接下来你再用gets输入字符串是不行的,gets会冲走缓冲区的回车符同时结束输入,就gg了,所以你必须在之前写个getchar()。但你用scanf输入字符串就可以不用getchar().。
scanf--scanf==scanf--getchar--gets
虽然我目前还不能完全解释这个中原理,但是有一点明白了,这个scanf也并不是什么时候都不读走回车符滴!
函数 | 回车符 | 空格和制表符 |
scanf() | 读入结束,留在缓冲区,要用getchar()将其读走 | 忽略,认为读入结束 |
gets() | 读入结束,冲走 | 读入,认为是字符串一部分 |