1.一开始没考虑到全部小于要找的数
思路:
排序,搜索
代码:
#include<iostream> #include<algorithm> #define MAX 10000 using namespace std; int main () { int n,i,q,x,number[MAX],cases = 0; while((cin >> n >> q ) && n && q) { cout<<"CASE# "<<++cases<<":"<<endl; for (int i=0; i<n; i++) { cin>>number[i]; } sort(number,number+n); while(q--) { cin>>x; if(number[n-1] < x) { cout<< x <<" not found"<<endl; continue; } for(int i=0; i<n; i++) { if(number[i] > x) { cout<< x <<" not found"<<endl; break; } else if (number[i] == x) { cout<< x <<" found at "<< ++i <<endl; break; } } } } return 0; }