黑马程序员技术交流社区
标题:
OC学习之折半查找
[打印本页]
作者:
zhuhoulin
时间:
2015-8-1 13:53
标题:
OC学习之折半查找
#include<stdio.h>
int zheban(int *num,int low,int heigh,int number){
int mid;
if((heigh-low) > 1){
mid = (low+heigh)/2;
if(number == num[mid]){
return mid;
}
else if(number > num[mid]){
zheban(num,mid+1,heigh,number);
}
else{
zheban(num,low,mid-1,number);
}
}
else if((heigh-low) == 1){
if(number == num[low]){
return low;
}
else if(number == num[heigh]){
return heigh;
}
else{
return -1;
}
}
else{
return low;
}
}
int main(){
int num[10] = {1,3,5,8,10,23,56,57,58,61};
int i;
int number;
int k;
scanf("%d",&number);
for(i = 0;i < 10;i++){
printf("%d ",num[i]);
}
printf("\n");
k = zheban(num,0,10,number);
if(k == -1){
printf("该数字不存在\n");
}
else{
printf("%d\n",k);
}
return 0;
}
作者:
四头
时间:
2015-8-2 11:47
谢谢楼主分享
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2