PAT A1141

思路清晰很重要

#include<iostream>  // 自己写的,耗时很长
#include<unordered_map>  
#include<vector>
#include<algorithm>
#include<cctype>
using namespace std;

struct Stu{
	string id;
	int score;
};
struct Ins{
	int order, total, num;
	string ins;
};

int n;
unordered_map<string, vector<Stu>> mp; // 保存机构中的学生 
unordered_map<string, Ins> mp2; // 保存机构中的具体数据,如总分,排名,人数 
vector<Ins> v;
bool cmp(const Ins& a, const Ins& b) {
	return a.total != b.total ? a.total > b.total : (a.num != b.num ? a.num < b.num : a.ins < b.ins);
}
int main() {
	cin >> n;
	string a, c;
	int b;
	for(int i = 0; i < n; i++) {
		cin >> a >> b >> c;
		for(int i = 0; i < c.size(); i++) if(isupper(c[i])) c[i] = tolower(c[i]);
		mp[c].push_back({a, b});
	}
	int mpSize = mp.size();
	cout << mpSize << endl;
	for(auto it : mp) {
		vector<Stu> temp = it.second;
		int scoreA = 0, scoreB = 0, scoreT = 0, tSize, totalW;
		tSize = temp.size();
		for(int i = 0; i < tSize; i++) {
			if(temp[i].id[0] == 'A') scoreA += temp[i].score;
		    else if(temp[i].id[0] == 'B') scoreB += temp[i].score;
		    else scoreT += temp[i].score;
		}
		totalW = scoreB / 1.5 + scoreA + scoreT * 1.5;
		Ins institude = {0, totalW, tSize, it.first}; 
		v.push_back(institude);
	}
	sort(v.begin(), v.end(), cmp);
	v[0].order = 1;
	for(int i = 1; i < v.size(); i++) {
		if(v[i].total != v[i-1].total) v[i].order = i + 1;
		else v[i].order = v[i - 1].order;
	}
	for(auto it : v) {
		cout << it.order << " " << it.ins << " " << it.total << " " << it.num << endl;
	}
	return 0;
} 

柳神代码

#include<iostream>
#include<unordered_map> 
#include<vector>
#include<algorithm>
#include<cctype>
using namespace std;

struct node{
	string school;
	int tws, ns;
};
int n;
unordered_map<string, int> cnt, sum;
vector<node> ins;
bool cmp(const node& a, const node& b) {
	return a.tws != b.tws ? a.tws > b.tws : (a.ns != b.ns ? a.ns < b.ns : a.school < b.school);
}
int main() {
	cin >> n;
	string a, c;
	int b;
	for(int i = 0; i < n; i++) {
		double scTotal = 0;
		cin >> a >> b >> c;
		for(int i = 0; i < c.size(); i++) c[i] = tolower(c[i]);
		if(a[0] == 'A') {
			scTotal += b;
		} else if(a[0] == 'B') {
			scTotal += b / 1.5;
		} else if(a[0] == 'T') {
			scTotal += b * 1.5;
		} 
		sum[c] += scTotal;
		cnt[c] ++;
	}
	for(auto it : cnt)
		ins.push_back({it.first, (int)sum[it.first], it.second});
	sort(ins.begin(), ins.end(), cmp);
	cout << ins.size() << endl;
	int rank, pres= -1;
	for(int i = 0; i < ins.size(); i++) {
		if(pres != ins[i].tws) rank = i+1;
		pres = ins[i].tws;    //   这种处理排名的方式值得熟记
		printf("%d %s %d %d\n", rank, ins[i].school.c_str(), ins[i].tws, ins[i].ns); 
	}
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值