请帮忙找小错误...简单冒泡排序

来源:百度知道 编辑:UC知道 时间:2024/05/31 16:24:48
#include <iostream>
using namespace std;
struct student
{
unsigned int rank;
unsigned int no;
unsigned int score;
};

int main()
{
int i,j,n;
student s[1001],temp;
cin>>n;

for (i=1;i<=n;i++)
{
cin>>s[i].no
>>s[i].score;
}

for (i=1;i<n;i++){
for (j=1;j<n-i;j++){
if (s[j].score<s[j+1].score)
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}

for (i=1;i<=n;i++)
{
cout<<i<<' '<<s[i].no<<' '<<s[i].score<<endl;
}
return 0;
}
Sample Input
5
1000 95
1001 100
1002 94
1006 95
1007 100
Sample Output
1 1001 100
1 1007 100
3 1000 95
3 1006 95
5 1002 94

但是我的输出:
1 1001 100
2 1000 95
3 1006 95
4 1002 94
5 1007 100

最后一位没比较到,改成这样试试
---------------------------
for (i=1;i<n;i++){
for (j=1;j<n-i+1;j++){ //这里应该一直比较到最后一位
if (s[j].score<s[j+1].score)
{
temp=s[j];
s[j]=s[j+1];
s[j+1]=temp;
}
}
}

额 很就前学的了 貌似 主函数 返回值一般是VOID 另外你的第一个FOR循环有点问题cin>>s[i].no
>>s[i].score no 和score 没有初值 最好把程序的文档写出来 这样才好找错误

恩,最后一位没比较,同意楼上的改法.