在左右相差为1数组中查找一个数

来源:互联网 发布:monkey社交软件下载 编辑:程序博客网 时间:2024/05/17 09:10

//这个用到跳查找的性质,比顺序查找效率高

#include <stdio.h>
void main(){
int a[25]={10,9,8,7,8,7,6,5,4,3,4,5,6,5,4,3,4,5,6,7,8,9,10,9,8};
int aid,i=0,num=0;
scanf("%d",&aid);
while(i>=0&&i<25)
{   num++;
   int temp=aid-a[i];
   if(!temp){
   printf("%d's positon is %d which run %d times ",a[i],i+1,num);
   break;
   }
   else
      i+=temp>0?temp:-temp;//这个地方要注意
}
if(i<0||i>=25)
printf("can't fine");

}