#include<stdio.h>
int main()
{
int array[10]={1,3,5,7,9,11,13,15,17,19};
int search;
int l_point=0;
//l_point为左下标,r_point为右下标;
int r_point=9;
printf("Input the number you want to search\n");
scanf("%d",&search);
if(search==array[r_point])
//因为除法是向下取整,因此,若查找的是最后一个数,下标达不到9,此处避免此种情况的死循环;
{
printf("Already exists,and it's the %dth number\n",r_point+1);
}
else
//同理,若查找的是第一个数,下标可以达到0,此种情况可以放在while循环中;
{
while(l_point!=r_point)
{
if(search>array[(l_point+r_point)/2])
{
l_point=(l_point+r_point)/2;
}
else if(search<array[(l_point+r_point)/2])
{
r_point=(l_point+r_point)/2;
}
else
{
break;
}
}
printf("Already exists,and it's the %dth number\n",(l_point+r_point)/2+1);
}
return(0);
}