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 41 42 43 44 45 46 47 48 49 50 51 | #include <stdio.h> #pragma warning(disable:4996) int n, N[50010], t, T[10010]; int Binary(int tar, int low, int high); int main() { FILE* fin = fopen("input.txt", "rt"); FILE* fout = fopen("output.txt", "wt"); fscanf(fin, "%d", &n); for (int i = 0; i < n; i++) fscanf(fin, "%d", &N[i]); fscanf(fin, "%d", &t); for (int i = 0; i < t; i++) { fscanf(fin, "%d", &T[i]); int result = Binary(T[i], 0, n); if (result == -1) fprintf(fout, "0\n"); else fprintf(fout, "%d\n", result+1); } fclose(fin); fclose(fout); return 0; } int Binary(int tar, int low, int high) { int mid = (low + high) / 2; if (low <= high) { if (N[mid] > tar) return Binary(tar, low, mid - 1); else if (N[mid] < tar) return Binary(tar, mid + 1, high); else if (N[mid] == tar) return mid; } return -1; } |