1141 PAT Ranking of Institutions

这个题就是要注意下总成绩的格式问题,统计的时候要用double进行累加,排序前要转换成去掉小数部分的int。

#include <iostream>
#include <vector>
#include <map>
#include <cctype>
#include <algorithm> 
using namespace std;
struct node{
	string school;
	int rank = 1, ns = 0; double tws = 0;
};
map<string, node> ma;
vector<node> v;
bool cmp(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() {
	int n; cin >> n;
	for(int i = 0; i < n; i++){
		string id, school; int score;
		cin >> id >> score >> school;
		for(int j = 0; j < school.length(); j++) school[j] = tolower(school[j]);
		if(id[0] == 'A') ma[school].tws += score;
		else if(id[0] == 'B') ma[school].tws += 1.0 * score / 1.5;
		else ma[school].tws += 1.5 * score;
		ma[school].school = school; ma[school].ns++;
	}
	for(auto it:ma){
		it.second.tws = (int)(it.second.tws); v.push_back(it.second);
	}
	sort(v.begin(), v.end(), cmp);
	printf("%d\n%d %s %d %d\n", v.size(), v[0].rank, v[0].school.c_str(), (int)v[0].tws, v[0].ns);
	for(int i = 1; i < v.size(); i++){
		if(v[i].tws == v[i-1].tws) v[i].rank = v[i-1].rank;
		else v[i].rank = i + 1;
		printf("%d %s %d %d\n", v[i].rank, v[i].school.c_str(), (int)v[i].tws, v[i].ns);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值