杭电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);