#include<stdio.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; struct student{ char regnum[100]; int score; int listnum; int rank; int localrank; }stu[30010]; bool cmp(student a,student b) { if(a.score!=b.score) return a.score>b.score; return strcmp(a.regnum,b.regnum)<0; } int main() { int n,k; scanf("%d",&n); int count=0; int n2=n; while(n--) { scanf("%d",&k); int temp=count; for(int i=1;i<=k;i++) { scanf("%s %d",stu[count].regnum,&stu[count].score); stu[count++].listnum=n2-n; } sort(stu+temp,count+stu,cmp); stu[temp].localrank=1; for(int i=temp+1;i<count;i++) { if(stu[i].score==stu[i-1].score) stu[i].localrank=stu[i-1].localrank; else stu[i].localrank=i-temp+1; } } sort(stu,stu+count,cmp); printf("%d\n",count); stu[0].rank=1; for(int i=1;i<count;i++) { if(stu[i].score==stu[i-1].score) stu[i].rank=stu[i-1].rank; else stu[i].rank=i+1; } for(int i=0;i<count;i++) printf("%s %d %d %d\n"This [the] .regnum, this [the] .rank, this [the] .listnum, this [the] .localrank); return 0 ; }
PAT A1025 sort of title template
sort(stu,stu+count,cmp)
stu is an array starting position, stu + count was the last plus a
Left right open closed
cmp inside a struct time is customizable. cmp is true of the condition is that the collation
Such return a.score> b.score sort is true does not move cmp, described at the time of a high score is to maintain the original state, it is described that a descending array, the highest score in front
Also note that the number of sorted by size in the case of the same fraction.
localrank is shot out of the process input in the future not to enter into another row. Direct sort (stu + temp, stu + count, cmp)