cs_xy @ 2021-03-16 19:39:39
#include<bits/stdc++.h>
using namespace std;
struct node {
int w,s,y,z,h;
}a[320];
bool cmp(node a,node b){
if(a.z==b.z) return a.w>b.w;
if(a.z==b.z&&a.w==b.w) return a.h<b.h;
else return a.z>b.z;
}
int main() {
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].w>>a[i].s>>a[i].y;
a[i].z=a[i].w+a[i].s+a[i].y;
a[i].h=i;
}
sort(a+1,a+1+n,cmp);
for(int i=1;i<=5;i++){
cout<<a[i].h<<" "<<a[i].z<<endl;
}
return 0;
}
by wsyhb @ 2021-03-16 19:58:22
@cs_xy 修改见代码:
#include<bits/stdc++.h>
using namespace std;
struct node {
int w,s,y,z,h;
}a[320];
bool cmp(node a,node b){
if(a.z==b.z&&a.w==b.w) return a.h<b.h;
if(a.z==b.z) return a.w>b.w;//这两行写反了
else return a.z>b.z;
}
int main() {
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].w>>a[i].s>>a[i].y;
a[i].z=a[i].w+a[i].s+a[i].y;
a[i].h=i;
}
sort(a+1,a+1+n,cmp);
for(int i=1;i<=5;i++){
cout<<a[i].h<<" "<<a[i].z<<endl;
}
return 0;
}
by ZBAA_MKC @ 2021-03-16 20:41:29
劝你还是用stable_sort()吧.......,sort是不稳定的排序算法,有可能因为排序算法不稳定造成的。(stable_sort()用法和普通sort完全一样)。
by ZBAA_MKC @ 2021-03-16 20:42:40
@cs_xy