PAT乙级1085 PAT单位排行

该代码实现了一个功能,根据输入的考生信息,计算并输出PAT考试中各个单位的加权总分及排名。首先使用map存储每个学校的考生数据,然后计算加权总分并排序。排序依据是加权总分降序,考生人数升序,单位码字典序。最后输出排名、学校、加权总分和考生人数。注意代码中使用了结构体和排序函数,并在最后释放了内存。
摘要由CSDN通过智能技术生成

1085 PAT单位排行 (25 分)

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

输入格式:

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

准考证号 得分 学校

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

输出格式:

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

排名 学校 加权总分 考生人数

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

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

输入样例:

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

注意i点,无非就是结构体的新建和修改问题,用map<学校名,学校>来判断读入的成绩是否已经创建,若map.find没找到,则新建结构体,若已经创建,则根据接收的成绩进行数据更新

最后记得释放指针,不然最后一个大数据测试点会超时。

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
struct school{
	int y = 0;
	int j = 0;
	int top = 0;
	int total = 0;
	string name;
	int stunum = 0;
};
bool cmp(school *a, school *b)
{
	if (a->total != b->total){
		return a->total > b->total;
	}
	else if (a->stunum != b->stunum)
	{
		return a->stunum<b->stunum;
	}
	else{
		return a->name<b->name;
	}
}
int main()
{   
	int N,score;
	cin >> N;
	string id,name;
	map<string, school*>schoollist;
	vector<school*>orderlist;
	char item;
	for (int i = 0; i < N; i++){
		cin >> id >> score >> name;
		item = id[0];
		transform(name.begin(), name.end(), name.begin(), ::tolower);//字符串的大小写转换函数
		if (schoollist.find(name) == schoollist.end()){
			school *sc = new school;
			schoollist[name] = sc;
		}
		if (item == 'B'){
			schoollist[name]->y = schoollist[name]->y + score;
		}
		else if (item == 'A'){
			schoollist[name]->j = schoollist[name]->j + score;
		}
		else if (item == 'T'){
			schoollist[name]->top = schoollist[name]->top + score;
		}
		schoollist[name]->stunum++;
		schoollist[name]->name=name;
	}
	
	for (auto it = schoollist.begin(); it != schoollist.end(); it++)
	{
		it->second->total = it->second->y / 1.5 + it->second->j + it->second->top*1.5;
		orderlist.push_back(it->second);
	}
	sort(orderlist.begin(),orderlist.end(),cmp);
	int order = 1,preoder=1;
	cout << orderlist.size() << endl;
	school *pre = orderlist[0];
	cout << preoder << " " << orderlist[0]->name << " " << orderlist[0]->total << " " << orderlist[0]->stunum << endl;
	for (int i = 1; i < orderlist.size(); i++){
		order++;
		if (orderlist[i]->total == pre->total)
		{
			cout << preoder << " " << orderlist[i]->name << " " << orderlist[i]->total << " " << orderlist[i]->stunum << endl;
		}
		else{
			preoder = order;
			cout << preoder << " " << orderlist[i]->name << " " << orderlist[i]->total << " " << orderlist[i]->stunum << endl;
			pre = orderlist[i];
		}
	}
	//释放内存
	for (auto it = schoollist.begin(); it != schoollist.end(); it++)
	{
		delete it->second;
	}
	 return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值