panghanchen2012 @ 2023-10-24 19:58:13
#include <iostream>
#include <algorithm>
using namespace std;
struct stu{
double ch,ma,en,sum;
int id;
};
bool cmp(stu a,stu b){
if(a.sum==b.sum and a.ch==b.ch){
return a.id<b.id;
}
if(a.sum==b.sum){
return a.ch>b.ch;
}
return a.sum>b.sum;
}
int main(){
stu a[300];
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i].ch>>a[i].ma>>a[i].en;
a[i].sum=a[i].ch+a[i].ma+a[i].en;
a[i].id=i+1;
}
sort(a+0,a+n,cmp);
for(int i=0;i<5;i++){
cout<<a[i].id<<" "<<a[i].sum<<endl;
}
return 0;
}
by panghanchen2012 @ 2023-10-24 20:02:47
得分50,测试点#6,#7,#8,#9,#10没过
by cinemaaa @ 2023-10-24 20:11:31
#include<bits/stdc++.h>
using namespace std;
const int MAX = 1010;
struct stu{
int s,ch,ma,eng;
int id;
}p[MAX];
bool cmp(stu a,stu b){
if(a.ch == b.ch && a.s == b.s)
return a.id < b.id;
if(a.s == b.s)
return a.ch>b.ch;
return a.s > b.s;
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>p[i].ch>>p[i].ma>>p[i].eng;
p[i].id = i+1;
p[i].s = p[i].ch + p[i].ma + p[i].eng;
}
sort(p,p+n,cmp);
for(int i=0;i<5;i++)
cout<<p[i].id<<' '<<p[i].s<<endl;
}
by xinxin2022 @ 2023-10-24 20:12:16
把“and”改成单独判断 如果不行那可能是“stu a[300]”中sum洛谷没有自动初始化,概率得不出正确答案 如果还不行的话检查一遍逻辑,发代码不加注释不是一个多好的习惯。
当然,最简单的方法就是看题解
by panghanchen2012 @ 2023-10-28 19:07:58
谢谢大佬,已AC