PAT甲级题 PAT单位排行 (字符串模拟 / 结构体+哈希表)

PAT单位排行

题目大意:
每次 PAT 考试结束后,考试中心都会发布一个考生单位排行榜。
本题就请你实现这个功能。

输入格式
输入第一行给出一个正整数 N,即考生人数。
随后 N 行,每行按下列格式给出一个考生的信息:

准考证号 得分 学校
其中 准考证号 是由 6 个字符组成的字符串,其首字母表示考试的级别:B 代表乙级,A 代表甲级,T 代表顶级;得分 是 [0,100] 区间内的整数;学校是由不超过 6 个英文字母组成的单位码(大小写无关)。
注意:题目保证每个考生的准考证号是不同的。

输出格式
首先在一行中输出单位个数。随后按以下格式非降序输出单位的排行榜:

排名 学校 加权总分 考生人数
其中 排名 是该单位的排名(从 1 开始);学校 是全部按小写字母输出的单位码;加权总分 定义为 乙级总分/1.5 + 甲级总分 + 顶级总分*1.5 的整数部分;考生人数 是该属于单位的考生的总人数。

学校首先按加权总分排行。如有并列,则应对应相同的排名,并按考生人数升序输出。如果仍然并列,则按单位码的字典序从小到大输出。

数据范围
1≤N≤105

输入样例:
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
输出样例:
5
1 cmu 192 2
1 au 192 3
3 pku 100 1
4 hypu 81 2
4 lanx 81 2

解题思路:
这个题比较麻烦,需要一步一步的模拟,详细备注见代码
要注意精度问题 +1e-8

Code:

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<unordered_set>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int maxn=1e5+7;

struct School{
	string name;
	int cnt;
	double sum;
	
	School():cnt(0),sum(0){} //初始化结构体,防止在hash表中直接给定非零的数
	
	bool operator < (const School &t) const{    //sort中重载运算符
		if(sum!=t.sum) return sum>t.sum;
		if(cnt!=t.cnt) return cnt<t.cnt;
		return name<t.name;
	}
};

int main()
{
    int n;
    unordered_map<string,School> hash;   //map类型hash表
    
    cin>>n;
    while(n--){
    	string id,sch;
    	double grade;
    	cin>>id>>grade>>sch;
    	for(auto &c:sch) c=tolower(c);         //转换成小写字母
    	
    	if(id[0]=='B') grade/=1.5;
    	else if(id[0]=='T') grade*=1.5;
    	
		hash[sch].sum+=grade;
		hash[sch].cnt++;
		hash[sch].name=sch;
	}
    vector<School> schools;
    for(auto it:hash){
    	it.second.sum=(int)(it.second.sum+1e-8);           // 前面B级/1.5可能会有精度问题
    	schools.push_back(it.second);
	}
    sort(schools.begin(),schools.end());
    cout<<schools.size()<<"\n";
    
    int rank=1;
    for(int i=0;i<schools.size();i++){
    	auto s=schools[i];
    	if(s.sum!=schools[i-1].sum) rank=i+1;
    	printf("%d %s %d %d\n",rank,s.name.c_str(),(int)s.sum,s.cnt);
	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43872264/article/details/107846289