- #include <stdio.h>
- int main() {
- int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
- int low = 0, high = 9;
- printf("输入您要查找的数\n");
- int b = 0;
- //要查找的数的位置
- int place = 0;
- scanf("%d",&b);
- while (high != low) {
-
- int middle = (high + low) / 2;
- if(b > a[middle])
- {
- if (b == a[high]) {
- place = high;
- break;
- }
- low = middle + 1;
- }else if (b < a[middle]){
- if (b == a[low]) {
- place = low;
- break;
- }
- high = middle - 1;
- }else{
- place = middle;
- break;
- }
-
- }
-
- printf("%d\n",place);
- return 0;
- }
复制代码 |
|