杭电acm1177

http://acm.hdu.edu.cn/showproblem.php?pid=1177

用qsort函数对结构体数组进行二级排序,先按题数降序,然后再按做题时间升序。最后在根据someone在整个名单中的排序和获奖名额判断名次

五、对结构体二级排序

struct Sample
{
int x;
int y;
}s[100];

//按照x从小到大排序,当x相等时按照y从大到小排序

int cmp( const void *a , const void *b )
{
struct Sample *c = (struct Sample *)a;
struct Sample *d = (struct Sample *)b;
if(c->x != d->x) return c->x - d->x;
else return d->y - c->y;

View Code
 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 struct stu{
 4            int bianhao;
 5            int num;
 6            int time;
 7           }ss[1000];
 8 int cmp( const void *a , const void *b )
 9 {
10 struct stu *c = (struct stu *)a;
11 struct stu *d = (struct stu *)b;
12 if(c->num != d->num) return d->num - c->num;
13 else return c->time - d->time;//二级排序,比一级多了if
14 }
15 int main()
16 {
17  int n,g,s,c,m,i,flag,g0,s0,c0;
18  while(scanf("%d%d%d%d%d",&n,&g,&s,&c,&m)&&(n||g||s||c||m))
19  {
20       for(i=0;i<n;i++)
21       {
22         scanf("%d %d:%d:%d",&ss[i].num,&g0,&s0,&c0);
23         ss[i].time=10000*g0+s0*100+c0;
24         ss[i].bianhao=i+1;
25       }
26       qsort(ss,n,sizeof(ss[0]),cmp);
27       for(i=0;i<n;i++)
28         if(ss[i].bianhao==m)
29         {
30          flag=i+1;
31          break;
32         }
33       if(flag<=g)
34         printf("Accepted today? I've got a golden medal :)\n");
35       else if(flag>g&&flag<=s+g)
36         printf("Accepted today? I've got a silver medal :)\n");
37       else if(flag>s&&flag<=s+g+c)
38         printf("Accepted today? I've got a copper medal :)\n");
39       else printf("Accepted today? I've got an honor mentioned :)\n");
40  }
41  return 0;
42 }

 


}

qsort(s,100,sizeof(s[0]),cmp);

转载于:https://www.cnblogs.com/huzhenbo113/articles/3003246.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值