1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 using namespace std; 5 struct score{ 6 int Ch; 7 int Ma; 8 int En; 9 int W; 10 int Tot; 11 int k; 12 }; 13 bool cmp(score x,score y) 14 { 15 if (x.Tot!=y.Tot) 16 return x.Tot>y.Tot; 17 if (x.Ma!=y.Ma) 18 return x.Ma>y.Ma; 19 if (x.W!=y.W) 20 return x.W>y.W; 21 if (x.Ch!=y.Ch) 22 return x.Ch>y.Ch; 23 if (x.En!=y.En) 24 return x.k<y.k; 25 } 26 score m[350]; 27 int main(){ 28 int n,a,b; 29 scanf("%d%d%d",&n,&a,&b); 30 for(int i=1;i<=n;i++){ 31 scanf("%d%d%d%d",&m[i].Ch,&m[i].Ma,&m[i].En,&m[i].W); 32 m[i].k=i; 33 m[i].Tot=m[i].Ma+m[i].W+m[i].Ch+m[i].En; 34 } 35 sort(m+1,m+n+1,cmp); 36 for (int i=a;i<=b;i++){ 37 printf("%d %d ",m[i].k,m[i].Tot); 38 } 39 return 0; 40 }
Tips:运用排序时,sort为从小到大排序,若在本题中不写bool cmp,会出现错误。
bool cmp及sort排序的代码如下:
1 bool cmp(score x,score y) 2 { 3 if (x.Tot!=y.Tot) 4 return x.Tot>y.Tot;}
1 sort(m+1,m+n+1,cmp); 2 for (int i=a;i<=b;i++){ 3 printf("%d %d ",m[i].k,m[i].Tot); 4 }
Tips:运用bool cmp时,需在sort内加上“cmp“,即
sort(m+1,m+n+1,cmp);
调用sort时需应用头文件#include<algorithm>