|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- void main()
- {
- int a[10]={1,2,3,4,5,6,7,8,9,10},l=0,m=5,h=9,f=0,i,n;
-
- printf("The array a is:\n");
- for(i=0;i<10;i++)
- {
- printf("%5d",a[i]);
- }
- putchar('\n');
- printf("Input the num of n:");
- scanf("%d",&n);
- while(l<=h) //循环条件这个问题很重要。
- {
- if(n==a[m])
- {
- f=1;
- break;
- }
- else if(n>a[m])
- {
- l=m+1;
- m=(l+h)/2;
- }
- else
- {
- h=m-1;
- m=(l+h)/2;
- }
- }
- if(f==1)
- {
- printf("%d is at the a[%d]\n",n,m);
- }
- else
- {
- printf("There is no %d\n",n);
- }
-
-
- }
复制代码 没事发着玩的,高手勿拍……
|
|