当先后输入1、3、4、12、23时,屏幕上出现 【11】 ;再输入12时,则屏幕上出现 【12】 。
#include <stdio.h>
#define N 5
main()
int i,j,number,top,bott,min,loca,a[N],flag;
char c;
printf("Please input 5 numbers (a[i]>a[i-1])\n");
scanf("%d",&a[0]);
i=1;
while (i<N)
scanf("%d",&a[i]);
if (a[i]>=a[i-1)) i++;
printf("\n");
for (i=0;i<N;i++) printf("%d",a[i]);
printf("\n");
flag=1;
while (flag)
scanf("%d",&number);
loca=0;
top=0;
bott=N-1;
if ((number<a[0])||(number>a[N-1])) loca=-1;
while ((loca==0) && (top<=bott))
min=(bott+top)/2;
if (number==a[min])
loca=min;
printf("%d is the %dth number\n",number,loca+1);
else if (number<a[min]) bott=min-1;
else top=min+1;
if (loca==0||loca==-1) printf("%d is not in the list.\n",number);
c=getchar();
if (c==’N’||c==’n’) flag=0;
参考答案:[11] 1 3 4 12 23 [12] 12 is the 4th number.