[Jobdu]查找学生信息(二分查找)

#include
#include
#include
#define maxn 10010
using namespace std;
typedef struct Node{
    char no[maxn];
    char name[200];
    char se[10];
    int age;
}node;
node pe[maxn];
int a[maxn],ha[maxn];
int n,m,x,num,t;
char s[maxn];
bool cmp(node a,node b){
     return strcmp(a.no,b.no)<0;
}
int main(){
    std::ios::sync_with_stdio(false);
    cin>>n;
    for(int i=0;i<n;i++){
        cin>>pe[i].no>>pe[i].name>>pe[i].se>>pe[i].age;
    }
    sort(pe,pe+n,cmp);
    cin>>t;
    int top,base,mid,flag=0;
    while(t--){
        cin>>s;
        flag=0;
        base=0;
        top=n;
        while(base<=top){
            mid=(base+top)/2;
            int tmp=strcmp(pe[mid].no,s);
            if(tmp>0){
                top=mid-1;
            }
            else if(tmp<0){
                base=mid+1;
            }
            else{
                flag=1;
                break;
            }
        }
        if(!flag) cout<<"No Answer!"<<endl;
        else{
            cout<<pe[mid].no<<" "<<pe[mid].name<<" "<<pe[mid].se<<" "<<pe[mid].age<<endl;
        }

    }
    return 0;
}

你可能感兴趣的:([Jobdu]查找学生信息(二分查找))