Chronostasis_WHY @ 2022-08-28 11:27:16
代码如下:
#include <iostream>
using namespace std;
int n = 0;
struct Students{
int chinese = 0;
int maths = 0;
int english = 0;
int all;
int id = 0;
};
int main(){
cin>>n;
Students stu[n];
for(int i = 0;i < n;i++){
stu[i].id = i + 1;
cin>>stu[i].chinese>>stu[i].maths>>stu[i].english;
stu[i].all = stu[i].chinese+stu[i].english+stu[i].maths;
}
for(int i = 0;i < n;i++){
for(int j = 0,m = 0;j < i;j++){
if(stu[j].all < stu[i].all){
m = stu[i].all;
stu[i].all = stu[j].all;
stu[j].all = m;
m = stu[i].id;
stu[i].id = stu[j].id;
stu[j].id = m;
}
}
}
for(int i = 0;i < 5;i++){
cout<<stu[i].id<<" "<<stu[i].all<<endl;
}
return 0;
}
by bamboo12345 @ 2022-08-28 11:32:58
@Chronostasis_WHY 您是不是无视了总分相等的可能性?
by Chronostasis_WHY @ 2022-08-28 11:38:21
if(stu[j].all <= stu[i].all){
if(stu[j].chinese < stu[i].chinese){
m = stu[i].all;
stu[i].all = stu[j].all;
stu[j].all = m;
m = stu[i].id;
stu[i].id = stu[j].id;
stu[j].id = m;
}
}
//因为这个代码拿了20分,我哪里错了