PAT 甲级1025 PAT Ranking的c++实现

时间:2020-05-19
本文章向大家介绍PAT 甲级1025 PAT Ranking的c++实现,主要包括PAT 甲级1025 PAT Ranking的c++实现使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>//这是sort函数所在的库
 4 using namespace std;
 5 struct Student{
 6     char id[15];          //id
 7     int score;            //分数 
 8     int location_number;//考场号 
 9     int location_rank;  //考场排名 
10 }stu[30010];
11 
12 bool cmp(Student student1,Student student2){
13     if(student1.score!=student2.score){
14         return student1.score>student2.score; 
15     }else{
16         return strcmp(student1.id,student2.id)<0;
17     }
18 }
19 
20 int main(){
21     int n;      //考场的数量 
22     int num;    //某个考场考生的人数
23     int k=0;     //考生总人数 
24     scanf("%d",&n);                //输入考场数量 
25     for(int i=0;i<n;i++){         
26         scanf("%d",&num);        //输入该考场的人数
27         for(int j=0;j<num;j++){
28             scanf("%s %d",stu[k].id,&stu[k].score);      //输入考生的ID和分数 
29             stu[k].location_number = i+1;              //获取考生的考场 
30             k++;
31         }
32         sort(stu+k-num,stu+k,cmp); //sort函数排序
33         stu[k-num].location_rank = 1;
34         for(int j=1;j<num;j++){
35             if(stu[k+j-num].score==stu[k+j-num-1].score){
36                 stu[k+j-num].location_rank = stu[k+j-num-1].location_rank;//同分和上一个同学同排名 
37             }else{
38                 stu[k+j-num].location_rank = j+1; //不同分就是当前循环的单位加1 
39             }
40         } 
41     }
42     sort(stu,stu+k,cmp);
43     printf("%d",k);//输出考生总人数 
44     int rank=1;    //当前排名    
45     printf("%s %d %d %d\n",stu[0].id,rank,stu[0].location_number,stu[0].location_rank);//只有一条数据时候输出一条 
46     for(int i=1;i<k;i++){
47         printf("%s ",stu[i].id);
48         if(stu[i].score==stu[i-1].score){
49             printf("%d %d %d\n",rank,stu[i].location_number,stu[i].location_rank);    
50         }else{
51             printf("%d %d %d\n",i+1,stu[i].location_number,stu[i].location_rank);
52             rank=i+1;
53         }          
54     }    
55 } 

原文地址:https://www.cnblogs.com/Jason-LinkStart/p/12919368.html