1025 PAT Ranking

代码如下:

 1 #include<iostream>
 2 #include<queue>
 3 #include<algorithm>
 4 #include<string>
 5 using namespace std;
 6 struct ran{                                 //定义出rank选手的需要的信息
 7     string number;                          //选手编号
 8     int bianhao;                            //组号
 9     int score;                              //成绩
10     int order;                              //组内排名
11     int order_total;                        //总体排名
12     friend bool operator <(ran r1,ran r2){  //重载小于号
13         if(r1.score < r2.score) return true;
14         else if(r1.score == r2.score && r2.number < r1.number) return true;
15         return false;
16     }
17 };
18 int main(){
19     priority_queue<ran> r;                  //优先队列进行保存
20     priority_queue<ran> r_2;
21     int n,m,k,sum = 0,paiming = 0,stem;     //paiming用来记录排名,stem用来记录排名的备份
22     cin >> n;                               //输入
23     ran rx;
24     for(int i = 0; i < n; i++){
25         cin >> m;
26         sum += m;
27         for(int j = 0; j < m; j++){
28             cin >> rx.number >> rx.score;   //输入
29             rx.bianhao = i+1;
30             r.push(rx);
31         }
32         paiming = 1,stem = 1;
33         while(!r.empty()){                  //计算组内排名
34             rx = r.top();                   //取出当前最高分
35             r.pop();                        //出队列
36             rx.order = paiming;             //paiming赋值
37             if(rx.score != r.top().score)   //有相同分数时的处理
38                 paiming = stem+1;
39             //else paiming++;
40             stem++;                         //备份一直自加
41             r_2.push(rx);                   //重新入队列
42         }
43     }
44     cout << sum << endl;
45     paiming = 1,stem = 1;
46     while(!r_2.empty()){
47         rx = r_2.top();
48         r_2.pop();
49         rx.order_total = paiming;
50         cout << fixed << rx.number << " " << rx.order_total << " " << rx.bianhao << " " << rx.order << endl;
51         if(rx.score != r_2.top().score) paiming = stem+1;
52         //else paiming++;
53         stem++;
54         //r.push(rx);
55     }
56 
57 //    for(int i = 0; i < sum; i++){
58 //        rx = r.top();
59 //        r.pop();
60 //        cout << rx.number << " " << rx.order_total << " " << rx.bianhao << " " << rx.order << endl;
61 //    }
62     //cout << "hello world";
63     return 0;
64 }

 

posted @ 2018-10-08 16:35  琥琥笙威  阅读(98)  评论(0编辑  收藏  举报