折半查找,帮忙找错
程序代码:
#include<stdio.h> #include<math.h> void main() { static int a[15]={2,6,8,9,12,23,34,45,56,67,78,89,90,123,456}; int i,j,m; scanf("%d",&m); for(j=0;j<15;j++) printf("%4d",a[j]); printf("\n"); i=7; while(fabs(i-7)<8) { if(m<a[7]) { if(a[i]-m==0) { printf("it is at (%d)\n",i+1); break; } i--; } else if(m>a[7]) { if(a[i]-m==0) { printf("it is at (%d) \n",i+1); break; } i++; } else printf("8\n"); } if(fabs(i-7)-8==0) printf("there is not\n"); }想了好久,