当先后输入1 3 4 12 23时 屏幕上出现【 】;再输入12时 则屏幕上出现【 】。 i
问题详情
当先后输入1、3、4、12、23时,屏幕上出现【 】;再输入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 numbern",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; } }
请帮忙给出正确答案和分析,谢谢!
参考答案
正确答案:1 3 4 12 2312 is the 4th number.
134122312isthe4thnumber.