|
|
发表于 2011-11-10 22:07:12
|
显示全部楼层
- #define M 10
- #include <stdio.h>
- #include <stdlib.h>
- int main()
- {
- static int a[M] = {-12,0,6,16,23,56,80,100,110,115};
- int n, low, mid, high, found,re;
- low = 0;
- high = M - 1;
- found = 0;
- printf("input a number to be searched:");
- re=scanf("%d", &n);
- if( n < a[0] || n > a[M-1] )
- {
- printf("there is no %d....",n);
- }
- for(; low <= high;)
- {
- mid = (low + high) / 2;
- if(a[mid] == n)
- {
- found = 1;
- break;
- }
- else if(n > a[mid])
- low = mid + 1;
- else
- high = mid - 1;
- }
- if(found == 1)
- printf("the index of %d is %d", n, mid);
- else if(re==1)
- printf("there is not %d",n);
- if(re!=1) printf("Wrong number!");
- system("PAUSE");
- return 0;
- }
复制代码 帮你写了下,大概可以…… |
|