#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;
} |
|