回复 15楼 czz5242199
确实。。。。已经超时了。。。用qsort排序试试。。
#include<iostream> #include<cstdlib> using namespace std; int cmp(const void *a,const void *b) { return *(int *)a-*(int *)b; } bool search(int key,int min,int max,int a[]) { int mid; while(min<=max) { mid = (min + max)/2; if(key == a[mid]) return true; else if(key < a[mid]) return search(key,min,mid-1,a); else if(key > a[mid]) return search(key,mid+1,max,a); } return false; //查找失败 } int main() { int i,n,m,key; int a[100000]; scanf("%d%d",&n,&m); for(i=0;i<n;i++) scanf("%d",&a[i]); qsort(a,100000,sizeof(a[0]),cmp); while(m--) { scanf("%d",&key); if(search(key,0,n-1,a) == true) printf("YES\n"); else printf("NO\n"); } return 0; }
#include<iostream> #include<cstdlib> using namespace std; int cmp(const void *a,const void *b) { return *(int *)a-*(int *)b; } bool search(int key,int min,int max,int a[]) { int mid; while(min<=max) { mid = (min + max)/2; if(key == a[mid]) return true; else if(key < a[mid]) return search(key,min,mid-1,a); else if(key > a[mid]) return search(key,mid+1,max,a); } return false; //查找失败 } int main() { int i,n,m,key; int a[100000]; scanf("%d%d",&n,&m); for(i=0;i<n;i++) scanf("%d",&a[i]); qsort(a,n,sizeof(a[0]),cmp); while(m--) { scanf("%d",&key); if(search(key,0,n-1,a)) printf("YES\n"); else printf("NO\n"); } return 0; }