[C++] PAT 1141 PAT Ranking of Institutions (25分)

在这里插入图片描述

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

题解:

直接建立unordered_map<string,node>在信息输入的时候直接存储每个学校的信息,存入的时候成绩使用double类型,最后比较的时候将成绩转化为int类型比较

#include <iostream>
#include <string>
#include <unordered_map>
#include <map>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;


struct node{
	double score;
	int num;
	string school;
};

int n;
set<string> sc_num;
unordered_map<string,node> mp;

bool cmp(node &a,node &b){
	if(int(a.score) != int(b.score)){
		return int(a.score) > int(b.score);
	}
	else if(a.num != b.num){
		return a.num < b.num;
	}
	else{
		return a.school < b.school;
	}

}

int main(){
	cin >> n;

	string temp_card,temp_school;
	double temp_score;
	
	for(int i=0;i<n;i++){
		cin >> temp_card;
		cin >> temp_score;
		cin >> temp_school;

		if(temp_card[0] == 'T'){
			temp_score = temp_score * 1.5;
		}
		else if(temp_card[0] == 'A'){
			temp_score = temp_score * 1.0;
		}
		else if(temp_card[0] == 'B'){
			temp_score = temp_score /1.5;
		}

		for(int j=0;j<temp_school.length();j++){
			temp_school[j] = tolower(temp_school[j]);
		}

		sc_num.insert(temp_school);

		if(mp.find(temp_school) == mp.end()){
			mp[temp_school].num = 1;
			mp[temp_school].score = temp_score;
			mp[temp_school].school = temp_school;
		}
		else{
			mp[temp_school].num += 1;
			mp[temp_school].score += temp_score;
		}

		

	}

	vector<node> ans;

	for(auto i:mp) ans.push_back(i.second);

	sort(ans.begin(),ans.end(),cmp);


	int index = 1;
	double last_socre = ans[0].score;


	cout << sc_num.size() << endl;

	for(int i=0;i<ans.size();i++){
		if(int(ans[i].score) != int(last_socre)){
			index = i+1;
		}

		printf("%d %s %d %d\n",index,ans[i].school.c_str(),int(ans[i].score),ans[i].num);
		
		last_socre = ans[i].score;
	}

	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值