标签:
#include <stdio.h> #include <string.h> //二分法 int binarySearch(int a[], int start, int end, int min); int main(void) { int a[100]; for (int i = 0; i<100; i++) a[i] = i; printf("the answer is %d\n",binarySearch(a, 0, 100, 0)); return 0; } int binarySearch(int a[], int start, int end, int min) { int mid = (start+end)/2; int result; if (a[mid] == min || start>end) { if (a[mid] == min) return mid; else return -1; } else if (min<a[mid]) return binarySearch(a, start, mid, min); else return binarySearch(a, mid, end, min); //return 不能缺少,否则 }
标签:
原文地址:http://www.cnblogs.com/sjdeak/p/4780638.html