标签:c
#include<stdio.h> int bin_search(int arr[], int start, int end, int search) { while (start <= end) { int mid = (start + end) / 2; if (arr[mid] > search) { end = mid - 1; } else if (arr[mid] < search) { start = mid + 1; } else { return mid; } } return -1; } int main() { int arr[10] = {0,1,2,3,4,5,6,7,8,9}; int m=0,ret=0; printf("输入所需查找的数m:"); scanf("%d",&m); ret = bin_search(arr, 0, sizeof(arr) / sizeof(arr[0])-1, m); if (ret == -1) { printf("查无此数!\n"); } else { printf("该数存在:%d\n",arr[ret]); } return 0; }
本文出自 “花开彼岸” 博客,请务必保留此出处http://zxtong.blog.51cto.com/10697148/1702241
标签:c
原文地址:http://zxtong.blog.51cto.com/10697148/1702241