hdu 5182 结构体排序

BC # 32 :

打 BC 的时候没看全三个关键字,WA 了五发,花了近一小时,问了一下才发现少看一个条件,于是顺利给跪。

题意:给出若干城市的两次空气质量,首先按空气质量差排序,若相等则按第二次排序,再相等则按输入顺序排。

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 
 6 struct p{
 7     int a,l,t;
 8 }a[200];
 9 
10 bool cmp(p a1,p a2){
11     if(a1.a==a2.a&&a1.t==a2.t) return a1.l<a2.l;
12     if(a1.a==a2.a) return a1.t<a2.t;
13     return a1.a>a2.a;
14 }
15 
16 int main(){
17     int n;
18     while(scanf("%d",&n)!=EOF){
19         int q,i;
20         for(q=0;q<n;q++){
21             int b,c;
22             scanf("%d%d",&b,&c);
23             a[q].l=q;a[q].a=b-c;a[q].t=c;
24         }
25         int j,t;
26         sort(a,a+n,cmp);
27         for(i=0;i<n;i++){
28             printf("%d",a[i].l);
29             if(i==n-1)printf("
");
30             else printf(" ");
31         }
32     }
33     return 0;
34 }
View Code
原文地址:https://www.cnblogs.com/cenariusxz/p/4330936.html