标签:col itblog color turn ima strong src image 技术
二分查找-------一般查找
二分查找
#include <stdio.h>
#define N 10
int f(int a[],int low,int high,int x)
{
int mid ;
while(low<=high)
{
mid=(low+high)/2 ;
if(a[mid]==x) return mid ;
else if(a[mid]<x) return f(a,mid+1,high,x) ;
else f(a,low,mid-1,x);
}
return -1 ;
}
int main ( )
{
int x, a[N]={ -1,12,23,42,56,65,81,92,100,109} ;
scanf("%d",&x);
printf("%d\n",f(a,0,N-1,x));
}
一般查找
#include <stdio.h>
int main ( )
{
int i,x, a[10]={ -1,12,23,42,56,65,81,92,100,109} ;
scanf("%d",&x);
for(i=0;i<10;i++)
if (a[i]>x) printf("%d",i-1);
return 0;
}
http://images.cnitblog.com/blog/528013/201312/23225000-ade3fdb4fefa4181847fc91cacdcfa53.png
标签:col itblog color turn ima strong src image 技术
原文地址:http://www.cnblogs.com/wangprince2017/p/7680374.html