牛客网PAT练兵场-德才论
题解:用sort排序即可
题目地址:https://www.nowcoder.com/questionTerminal/97b6a49a85944650b2e3d0660b91c324
1 /** 2 * Copyright(c) 3 * All rights reserved. 4 * Author : Ycute 5 * Date : 2019-10-30-19.49.12 6 * Description : struct排序 7 */ 8 #include<iostream> 9 #include<cstdio> 10 #include<cmath> 11 #include<cstring> 12 #include<algorithm> 13 using namespace std; 14 15 16 struct stu{ 17 int id; 18 int c1; 19 int c2; 20 }; 21 22 bool cmp(struct stu a,struct stu b){ 23 if((a.c1+a.c2)==(b.c1+b.c2)){ 24 if(a.c1==b.c1){ 25 return a.id<b.id?true:false; 26 }else{ 27 return a.c1>b.c1?true:false; 28 } 29 }else{ 30 return (a.c1+a.c2)>(b.c1+b.c2)?true:false; 31 } 32 } 33 34 struct stu p1[100005]; 35 struct stu p2[100005]; 36 struct stu p3[100005]; 37 struct stu p4[100005]; 38 int main(){ 39 int num,C1,C2; 40 scanf("%d %d %d",&num,&C1,&C2); 41 int tot=0; 42 int l1=0,l2=0,l3=0,l4=0; 43 for(int i=0;i<num;++i){//输入并进行分组 44 int a,b,c; 45 scanf("%d %d %d",&a,&b,&c); 46 if(b<C1||c<C1)continue; 47 if(b>=C2&&c>=C2){ 48 p1[l1].id=a; 49 p1[l1].c1=b; 50 p1[l1++].c2=c; 51 } else if(b>=C2){ 52 p2[l2].id=a; 53 p2[l2].c1=b; 54 p2[l2++].c2=c; 55 }else if(b>=c){ 56 p3[l3].id=a; 57 p3[l3].c1=b; 58 p3[l3++].c2=c; 59 }else{ 60 p4[l4].id=a; 61 p4[l4].c1=b; 62 p4[l4++].c2=c; 63 } 64 ++tot; 65 } 66 //排序 67 sort(p1,p1+l1,cmp); 68 sort(p2,p2+l2,cmp); 69 sort(p3,p3+l3,cmp); 70 sort(p4,p4+l4,cmp); 71 //cout<<l1<<" "<<l2<<" "<<l3<<" "<<l4<<endl; 72 printf("%d\n",tot); 73 for(int i=0;i<l1;++i){ 74 printf("%d %d %d\n",p1[i].id,p1[i].c1,p1[i].c2); 75 } 76 for(int i=0;i<l2;++i){ 77 printf("%d %d %d\n",p2[i].id,p2[i].c1,p2[i].c2); 78 } 79 for(int i=0;i<l3;++i){ 80 printf("%d %d %d\n",p3[i].id,p3[i].c1,p3[i].c2); 81 } 82 for(int i=0;i<l4;++i){ 83 printf("%d %d %d\n",p4[i].id,p4[i].c1,p4[i].c2); 84 } 85 return 0; 86 }