Gaozhongsheng @ 2023-06-05 17:45:44
#include<bits/stdc++.h>
using namespace std;
struct student{
int id;
int Chinese,Math,English;
int sum;
}stu[310];
bool cmp(student a,student b){
return a.sum>b.sum;
}
int main(){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
stu[i].id=i;
cin>>stu[i].Chinese>>stu[i].Math>>stu[i].English;
stu[i].sum=stu[i].Chinese+stu[i].Math+stu[i].English;
}
sort(stu+1,stu+n+1,cmp);
for(int i=1;i<=n;i++){
if(stu[i].sum==stu[i+1].sum){
//总分相同语文成绩<后面,学号互换
if(stu[i].Chinese<stu[i+1].Chinese){
int cnt=stu[i].id;
stu[i].id=stu[i+1].id;
stu[i+1].id=cnt;
}
if(stu[i].Chinese==stu[i+1].Chinese&&stu[i].id>stu[i+1].id){
//都一样学号小的在前面
int opt=stu[i].id;
stu[i].id=stu[i+1].id;
stu[i+1].id=opt;
}
}
}
for(int i=1;i<=5;i++){
printf("%d %d\n",stu[i].id,stu[i].sum);
}
return 0;
}
by csx777 @ 2023-06-05 18:01:50
可能会有很多个总分相同的,但您只比较了相邻两人的语文成绩
by Gaozhongsheng @ 2023-06-05 20:17:49
@csx777 蟹蟹蟹蟹
by HenryQ2022 @ 2023-06-11 14:34:04
建议用sort
//cmp函数
bool cmp(child x, child y) {
if(x.sum != y.sum) //总分
return x.sum > y.sum;
else if(x.ch != y.ch) //语文
return x.ch > y.ch;
else //学号
return x.num < y.num;
}
//sort
sort(a + 1, a + n + 1, cmp);
//结构体捆绑排序