#include #include int search(int* a, int key){ int low = 0; int high = 14; int mid; while (low <= high) { mid = (low + high) / 2; if (key == a[mid]) return mid; else if (key < a[mid]) high = mid - 1; else low = mid + 1; } return 0;}int main(){ int* a, key, i = 0; int b[15] = { 0 }; a = b; printf("盯兄please input all the 15 data(from small to big) \配隐n "); for (i = 0; i < 15; i++) { scanf("&d", &b[i]); //这里是b[i] printf("\n"); } printf("please input the key"); scanf("%d", &key); if (!search(a, key)) printf("there is no data you are searching for \n"); else //这个else不能少,不然你怎培则厅么运行都会输出下面的语句 { printf("the data you are searching for is in the %dth place\n", search(a, key)); } getchar(); return 0;}
标签:C语言,二分,查找
版权声明:文章由 知识百问 整理收集,来源于互联网或者用户投稿,如有侵权,请联系我们,我们会立即处理。如转载请保留本文链接:https://www.zhshbaiwen.com/answer/182157.html