題目:1025. PAT Ranking (25)css
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct record
{
char renumber[16];
int finalrank;
int localnum;
int localrank;
int score;
}s[30005];
int comp(const void*a,const void*b)
{
struct record *p=(struct record*)a;
struct record *q=(struct record*)b;
if(p->score!=q->score)
return q->score-p->score;
else
return strcmp(p->renumber,q->renumber);
}
int main()
{
int n,size=0,i;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
int k,j;
scanf("%d",&k);
for(j=size;j<size+k;j++)
{
scanf("%s %d",s[j].renumber,&s[j].score);
s[j].localnum=i;
}
qsort(&s[size],k,sizeof(struct record),comp);
for(j=size;j<size+k;j++)
{
if(j==size)
s[j].localrank=j-size+1;
else
{
if(s[j].score==s[j-1].score)
s[j].localrank=s[j-1].localrank;
else
s[j].localrank=j-size+1;
}
}
size+=k;
}
qsort(s,size,sizeof(struct record),comp);
int j;
for(j=0;j<size;j++)
{
if(j==0)
s[j].finalrank=j+1;
else
{
if(s[j].score==s[j-1].score)
s[j].finalrank=s[j-1].finalrank;
else
s[j].finalrank=j+1;
}
}
printf("%d\n",size);
for(i=0;i<size;i++)
printf("%s %d %d %d\n",s[i].renumber,s[i].finalrank,s[i].localnum,s[i].localrank);
return 0;
}