1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| #include <cstdio> #include <iostream> #include <limits.h> using namespace std;
int n;
int finds(int a[], int k) { int l = 0, r = n - 1, x = 0; while(l <= r){ x = (l + r) / 2; if(a[x] < k) l = x + 1; else if(a[x] > k) r = x - 1; else break; } if(l <= r) return x + 1; else return -1; }
int main() { int a[100]; int m, t; while(scanf("%d", &n) != EOF){ for(int i = 0; i < n; i++) scanf("%d", &a[i]); scanf("%d", &t); while(t--){ scanf("%d", &m); printf("%d\n", finds(a, m)); } } }
|