二分查找-------一般查找
二分查找 #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; }