#include <stdio.h> int half_search(int arr[], int first, int last, int n) { int mid = 0; while (first <= last) { mid = (first + last) / 2; if (n < arr[mid]) { last = mid - 1; } else if (n>arr[mid]) { first = mid + 1; } else { return 1; } } return -1; } int main() { int arr[] = { 1, 3, 5, 7, 9, 11, 13, 15, 17, 19 }; int i; while (1) { scanf("%d", &i); if ((half_search(arr, 0, sizeof(arr) / sizeof(arr[0]) - 1, i) == -1)) { printf("not exist\n"); } else { printf("%d\n", i); } } return 0; }
时间: 2024-09-30 06:23:41