ZJU PAT 1025 PAT Ranking

http://pat.zju.edu.cn/contests/pat-practise/1025

超级无聊的模拟题。

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <string.h>
 4 using namespace std;
 5 int N;
 6 int K;
 7 struct Student{
 8     char name[14];
 9     int score;
10     int loc_number;
11     int loc_rank;
12 }stu[30000];
13 bool cmp(struct Student a,struct Student b)
14 {
15     if(a.score!=b.score){
16         return a.score>b.score;
17     }
18     return strcmp(a.name,b.name)<0;
19 }
20 
21 int main()
22 {
23     scanf("%d",&N);
24     int i,k=-1;
25     int sum=0;
26     for(i=0;i<N;i++){
27         scanf("%d",&K);
28         sum+=K;
29         int j;
30         for(j=0;j<K;j++){
31             scanf("%s%d",&stu[k+1].name,&stu[k+1].score);
32             stu[1+k].loc_number=i+1;
33             k++;
34         }
35 
36         sort(stu+1+k-K,stu+k+1,cmp);
37         stu[k-K+1].loc_rank=1;
38         int pre=1;
39         int d=1;
40         for(j=k-K+2;j<=k;j++){
41             if(stu[j].score==stu[j-1].score){
42                 stu[j].loc_rank=pre;
43                 d++;
44             }
45             else    {
46                 stu[j].loc_rank=pre+d;
47                 pre+=d;
48                 d=1;
49             }
50         }
51     }
52     sort(stu,stu+k+1,cmp);
53 
54     i=0;
55     printf("%d\n",sum);
56     printf("%s %d %d %d\n",stu[i].name,1,stu[i].loc_number,stu[i].loc_rank);
57     int d=1;
58     int pre=1;
59     for(i=1;i<=k;i++){
60         if(stu[i].score==stu[i-1].score){
61             printf("%s %d %d %d\n",stu[i].name,pre,stu[i].loc_number,stu[i].loc_rank);
62             d++;
63         }else{
64             printf("%s %d %d %d\n",stu[i].name,pre+d,stu[i].loc_number,stu[i].loc_rank);
65             pre+=d;
66             d=1;
67         }
68     }
69 }
原文地址:https://www.cnblogs.com/yangce/p/2448997.html