二维数组--二分法:数组a中的数值是按照由小到大的顺序排列的:-12,0,6,16,23,56,80,100,110,115,从键盘上输入一个数判断该数是否在数组中若在则输入所在序号(有问题)


#include "stdio.h"
#define M 10
void main()
{
    int a[M]={-12,0,6,16,23,56,80,100,110,115};
    int mid,high,low,n,found;
    low=0;
    high=M-1;
    found=0;
    printf("Input a number to be searcher:");
    scanf("n=%d",&n);
    while(low<=high)
    {
        mid=(high+low)/2;
        if(n==a[mid])
        {
          found=1;
          break;
        }
        else if(n>a[mid])
        {
          low=mid+1;
        }
        else
        {
          high=mid-1;
        }
    }
    if(found==1)
    {
      printf("The index of %d id %d",n,mid);
    }
    else
    {
      printf("There is not %d",n);
    }
}

 

posted @ 2020-02-06 15:32  七只蚂蚁  阅读(493)  评论(0)    收藏  举报