首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

冒泡排序法的异常

2012-10-14 
冒泡排序法的错误题目要求对12个运动员的成绩进行排序。我的程序有错,请指教C/C++ code#includeiostream#

冒泡排序法的错误
题目要求对12个运动员的成绩进行排序。
我的程序有错,请指教

C/C++ code
#include<iostream>#include<string>using namespace std;struct Athlete{    string number;//运动员号    float grade;//成绩};int main(){    Athlete a[12]={{"001",13.6},{"002",14.8},{"010",12.0},            {"011",12.7},{"023",15.6},{"025",13.4},            {"031",14.9},{"036",12.6},{"037",13.4},            {"102",12.5},{"325",15.3},{"438",12.7}};    for(int i=0;i!=12;++i)        for(int j=0;j!=11-i;++j)        {            if(a[j+1].grade<a[j].grade)            {                Athlete temp;                temp=a[j];                a[j]=a[j+1];                a[j+1]=temp;            }        }        //for(int i=0;i!=12;++i)            //cout<<athletes[i].grade<<endl;    return 0;}


[解决办法]
冒泡没错 输出改下
C/C++ code
for(int i=0;i!=12;++i)cout<<a[i].grade<<endl;
[解决办法]
C/C++ code
for(int i=0;i<12;++i){  int flag=1;  for(int j=0;j<12-i;++j)  {  if(a[j+1].grade<a[j].grade)  {   flag=0;  Athlete temp;  temp=a[j];  a[j]=a[j+1];  a[j+1]=temp;  }  if(flag) break;  }} 

热点排行