PAT | A1141 PAT Ranking of Institutions

这道题如果在每一次读取的时候更新分数就把它强制转换成int,测试点5会答案错误

#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
#include <algorithm>

using namespace std;

struct School{
	string name;
	double score;
	int finalScore;
	int testees;
	School(string n){
		name = n;
		score = 0;
		finalScore = 0;
		testees = 1;
	}
	School(){}
};

char op(char ch){
	return (ch >= 'A' && ch <= 'Z') ? ch + 'a' - 'A' : ch;
}

string toLowerCase(string s){
	int len = s.length();
	for(int i = 0;i < len;i++)
		s[i] = op(s[i]);
	return s;
}

unordered_map<string,int> strToIndex; // 学校名在数组中对应的编号
vector<School> schools;

bool cmp(School a,School b){
	if(a.finalScore != b.finalScore)
		return a.finalScore > b.finalScore;
	else if(a.testees != b.testees)
		return a.testees < b.testees;
	else
		return a.name < b.name;
}

int main(){
	int n;
	scanf("%d",&n);
	for(int i = 0;i < n;i++){
		string id,school;
		int score;
		cin>>id>>score>>school;
		school = toLowerCase(school);
		int index;
		if(strToIndex.find(school) == strToIndex.end()){
			schools.push_back(School(school));
			index = schools.size() - 1;
			strToIndex[school] = index;
		}else{
			index = strToIndex[school];
			schools[index].testees++;
		}
		if(id[0] == 'T')
			schools[index].score += 1.5 * score;
		else if(id[0] == 'A')
			schools[index].score += score;
		else
			schools[index].score += score / 1.5;
		schools[index].finalScore = schools[index].score;
	}
	sort(schools.begin(),schools.end(),cmp);
	int lastrank = 1;
	printf("%d\n",schools.size());
	printf("1 %s %d %d\n",schools[0].name.c_str(),schools[0].finalScore,schools[0].testees);
	for(int i = 1;i < schools.size();i++){
		if(schools[i].finalScore == schools[i - 1].finalScore)
			printf("%d %s %d %d\n",lastrank,schools[i].name.c_str(),schools[i].finalScore,schools[i].testees);
		else{
			printf("%d %s %d %d\n",i + 1,schools[i].name.c_str(),schools[i].finalScore,schools[i].testees);
			lastrank = i + 1;
		}
	}
	system("pause");
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值