题目很简单,加上读题20分钟一次AC。还是用到了快排qsort。
#include<iostream> #include<cstdlib> using namespace std; int cmp(const void* a,const void* b) { return *(int*)a-*(int*)b; } int main() { int n,q; int col=0; while(cin>>n>>q&&n!=0) { col++; int* data=new int[n]; for (int i=0;i<n;i++) { cin>>data[i]; } qsort(data,n,sizeof(data[0]),cmp); cout<<"CASE# "<<col<<":"<<endl; while(q--) { int query,index; index=0; cin>>query; for (int j=0;j<n;j++) { if (data[j]==query) { index=j+1; break; } } if (index==0) cout<<query<<" not found"<<endl; else cout<<query<<" found at "<<index<<endl; } delete data; } return 0; }