PAT_甲級_1141 PAT Ranking of Institutions (25point(s)) (C++)【模擬/結構體排序】

目錄

1,題目描述

題目大意

2,思路

數據結構

算法

3,AC代碼

4,解題過程

第一搏

第二搏


1,題目描述

Sample Input:

10
A57908 85 Au
B57908 54 LanX
A37487 60 au
T28374 67 CMU
T32486 24 hypu
A66734 92 cmu
B76378 71 AU
A47780 45 lanx
A72809 100 pku
A03274 45 hypu

 

Sample Output:

5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2

題目大意

每個學校有若干學生參與PAT不同等級的測試,現在要根據考生的成績對學校/機構進行排名,排名規則如下:

 根據TWS(B級score/1.5,A級score*1.0,T級score*1.5,每個機構的所有考生的成績之和,最後取整!)進行排序,TWS相同(取整後)的機構排名相同;

若排名相同,輸出順序按照Ns(各機構參賽人數)增序輸出;

若Ns仍相同,輸出順序按照機構代碼字母表順序輸出;

 

2,思路

數據結構

  • struct node{
        string school;
        int Ns = 0, tws;
        double TWS = 0;
    };TWS:累加時的成績,爲保證精度,用double表示;tws:TWS的整數部分,在排序之前要對TWS取整;
  • unordered_map<string, node> record:統計各個機構的情況;
  • vector<node> ans:存放最終結果;

算法

  1. 獲取數據,計算TWS,統計Ns;
  2. 遍歷map,對TWS取整(測試點5),並將各個機構的情況放入ans中,並排序:
  3. 計算排名並輸出(index記錄當前機構下標(從1開始),rank表示機構排名):

 

3,AC代碼

#include<bits/stdc++.h>
using namespace std;
struct node{
    string school;
    int Ns = 0, tws;
    double TWS = 0;
};
unordered_map<string, node> record;
vector<node> ans;
bool cmp1(node a, node b){
    if(a.tws != b.tws) return a.tws > b.tws;
    else{
        if(a.Ns != b.Ns) return a.Ns < b.Ns;
        else return a.school < b.school;
    }
}
int main(){
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
    int N;
    double score;
    cin>>N;
    string id, sch;
    while(N--){
        cin>>id>>score>>sch;
        transform(sch.begin(), sch.end(), sch.begin(), ::tolower);
        if(id[0] == 'T') score *= 1.5;
        else if(id[0] == 'B') score /= 1.5;
        else score *= 1.0;
        record[sch].school = sch;
        record[sch].Ns++;
        record[sch].TWS += score;
    }
    for(auto it : record){
        it.second.tws = it.second.TWS;
        ans.push_back(it.second);
    }
    sort(ans.begin(), ans.end(), cmp1);
    int rank = 1, index = 1;
    int lastTWS = ans[0].tws;// !!!是TWS的整數部分
    cout<<ans.size()<<endl;
    for(auto it : ans){
        if(it.tws != lastTWS){
            rank = index;
            lastTWS = it.tws;
        }
        index++;
        printf("%d %s %d %d\n", rank, it.school.c_str(), it.tws, it.Ns);
    }
    return 0;
}

4,解題過程

第一搏

先存入map<string, node>中,再遍歷map存入vector<node>中,排序,計算排名,輸出。一氣呵成

#include<bits/stdc++.h>
using namespace std;
struct node{
    string school;
    int Ns = 0;
    double TWS = 0;
};
unordered_map<string, node> record;
vector<node> ans;
bool cmp1(node a, node b){
    if(a.TWS != b.TWS) return a.TWS > b.TWS;
    else{
        if(a.Ns != b.Ns) return a.Ns < b.Ns;
        else return a.school < b.school;
    }
}
int main(){
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif // ONLINE_JUDGE
    int N;
    double score;
    cin>>N;
    string id, sch;
    while(N--){
        cin>>id>>score>>sch;
        transform(sch.begin(), sch.end(), sch.begin(), ::tolower);
        if(id[0] == 'T') score *= 1.5;
        else if(id[0] == 'B') score /= 1.5;
        record[sch].school = sch;
        record[sch].Ns++;
        record[sch].TWS += score;
    }
    for(auto it : record){
        ans.push_back(it.second);
    }
    sort(ans.begin(), ans.end(), cmp1);
    int rank = 1, index = 1;
    int lastTWS = ans[0].TWS;// !!!不是double
    cout<<ans.size()<<endl;
    for(auto it : ans){
        int tws = it.TWS;
        if(tws != lastTWS){
            rank = index;
            lastTWS = it.TWS;
        }
        index++;
        printf("%d %s %d %d\n", rank, it.school.c_str(), tws, it.Ns);
    }
    return 0;
}

第二搏

測試點5冥思苦想不得解。。。上網查詢後得到大佬的指點,原文戳這裏@樂行僧丶【PAT(甲級)1141.PAT Ranking of Institutions (25 分)】

關於TWS有兩點需要注意:

1,累加時要用float/double,避免損失精度;

2,排序時要將其取整,並針對整型的tws排序,這纔是題目的規則;

於是我在結構體中加入了int型變量tws,在遍歷map將node存入vector中時,對TWS取整存入tws,並對tws排序。

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章