| 
 | 
 
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册  
 
x
 
- // 6.cpp : Defines the entry point for the console application.
 
 - //
 
  
- #include "stdafx.h"
 
 - #include "stdio.h"
 
 - #include "math.h"
 
 - void main()
 
 - {
 
 -         int a[90],i,j,t,m,d,x,b,k; 
 
 -         printf("input 9 counts:\n");
 
 -         for(i=0;i<9;i++)
 
 -         {
 
 -                 scanf("%d",&a[i]);
 
 -         }
 
 -         for(i=0;i<8;i++)
 
 -         {
 
 -                 m=i;
 
 -                 for(j=i+1;j<9;j++)
 
 -                 {
 
 -                         if(a[m]<a[j])        m=j;
 
 -                         if(i!=m)        {t=a[i];a[i]=a[j];a[j]=t;}
 
 -                 }
 
 -         }
 
  
-         for(i=0;i<9;i++)
 
 -                 printf("%4d",a[i]);
 
 -         printf("\n");
 
 -         printf("find number is:\n");
 
 -         scanf("%d",&x);
 
 -         b=0;d=8;
 
 -         while(b<=d)
 
 -         {
 
 -                 m=(b+d)/2;
 
 -                 if(x=a[m]) break;
 
 -                 if(x>a[m]) b=m+1;
 
 -                 else d=m-1;
 
 -         }
 
 -         if(b<=d)
 
 -                 printf("要查找的数据是第%d个\n",(m+1));
 
 -         else printf("errro!");
 
  
- }
 
  
 
  复制代码 
 
 
结果为什么要查找的数据都是第5个??
 本帖最后由 枫还 于 2019-2-8 08:44 编辑 
你这排序有问题,你想排成从大到小吗? 
冒泡排序是排成从小到大,否则你应该从a[8]开始向a[0]判断,不然会出问题,比如第一次a[0]<a[1],交换之后,第二次循环就略过了a[0],而a[0]并不一定是最大的 
而且你下面的while又是默认数组已经从小到大排好了,你理下思路先
  
 
 
 |   
 
 
 
 |