代码:
//只要对输入的数据排序,然后查找即可(可用二分,更快) #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <list> #include <vector> #include <stack> #include <cmath> #include <algorithm> using namespace std; const int MAXN = 10010; int n , q; int arrN[MAXN]; int arrQ[MAXN]; int numQ[MAXN]; int main(){ int cnt = 1; while(scanf("%d%d%*c" , &n , &q) &&q &&n){ for(int i = 0 ; i < n ; i++) scanf("%d" , &arrN[i]); sort(arrN , arrN+n); memset(arrQ , 0 ,sizeof(arrQ)); for(int i = 0 ; i < q ; i++){ scanf("%d" , &numQ[i]); for(int j = 0 ; j < n ;j++){ if(numQ[i] == arrN[j]){ arrQ[numQ[i]] = j+1; break; } } } printf("CASE# %d:\n" , cnt); for(int i = 0 ; i < q ; i++){ if(arrQ[numQ[i]]){ printf("%d found at %d\n" , numQ[i] , arrQ[numQ[i]]); } else printf("%d not found\n" , numQ[i]); } ++cnt; } return 0; }
时间: 2024-10-24 20:54:18