目录

​1,题目描述​

​题目大意​

​2,思路​

​数据结构​

​算法​

​3,AC代码​

​4,解题过程​

​第一搏​

​第二搏​


1,题目描述

PAT_甲级_1141 PAT Ranking of Institutions (25point(s)) (C++)【模拟/结构体排序】_1141

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相同(取整后)的机构排名相同;

若排名相同,输出顺序按照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. PAT_甲级_1141 PAT Ranking of Institutions (25point(s)) (C++)【模拟/结构体排序】_1141_02

  3. 遍历map,对TWS取整(测试点5),并将各个机构的情况放入ans中,并排序:
  4. PAT_甲级_1141 PAT Ranking of Institutions (25point(s)) (C++)【模拟/结构体排序】_C++_03

  5. 计算排名并输出(index记录当前机构下标(从1开始),rank表示机构排名):
  6. PAT_甲级_1141 PAT Ranking of Institutions (25point(s)) (C++)【模拟/结构体排序】_C++_04

 

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;
}

PAT_甲级_1141 PAT Ranking of Institutions (25point(s)) (C++)【模拟/结构体排序】_1141_05

第二搏

测试点5冥思苦想不得解。。。上网查询后得到大佬的指点,原文戳这里​​@乐行僧丶【PAT(甲级)1141.PAT Ranking of Institutions (25 分)】​

关于TWS有两点需要注意:

1,累加时要用float/double,避免损失精度;

2,排序时要将其取整,并针对整型的tws排序,这才是题目的规则;

于是我在结构体中加入了int型变量tws,在遍历map将node存入vector中时,对TWS取整存入tws,并对tws排序。

PAT_甲级_1141 PAT Ranking of Institutions (25point(s)) (C++)【模拟/结构体排序】_C++_06

PAT_甲级_1141 PAT Ranking of Institutions (25point(s)) (C++)【模拟/结构体排序】_PAT_07

PAT_甲级_1141 PAT Ranking of Institutions (25point(s)) (C++)【模拟/结构体排序】_模拟 结构体排序_08

PAT_甲级_1141 PAT Ranking of Institutions (25point(s)) (C++)【模拟/结构体排序】_1141_09

PAT_甲级_1141 PAT Ranking of Institutions (25point(s)) (C++)【模拟/结构体排序】_C++_10