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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_37360631/article/details/88084669
#include<iostream>
#include<cstring>
#include<algorithm>
#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;
}

猜你喜欢

转载自blog.csdn.net/qq_37360631/article/details/88084669