1141 PAT Ranking of Institutions PAT甲级

Posted zxzmnh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1141 PAT Ranking of Institutions PAT甲级相关的知识,希望对你有一定的参考价值。

After each PAT, the PAT Center will announce the ranking of institutions based on their students’ performances. Now you are asked to generate the ranklist.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=105), which is the number of testees. Then N lines follow, each gives the information of a testee in the following format:
ID Score School
where “ID” is a string of 6 characters with the first one representing the test level: “B” stands for the basic level, “A” the advanced level and “T” the top level; “Score” is an integer in [0, 100]; and “School” is the institution code which is a string of no more than 6 English letters (case insensitive). Note: it is guaranteed that “ID” is unique for each testee.
Output Specification:
For each case, first print in a line the total number of institutions. Then output the ranklist of institutions in nondecreasing order of their ranks in the following format:
Rank School TWS Ns
where “Rank” is the rank (start from 1) of the institution; “School” is the institution code (all in lower case); “TWS” is the total weighted score which is defined to be the integer part of “ScoreB/1.5 + ScoreA + ScoreT*1.5”, where “ScoreX” is the total score of the testees belong to this institution on level X; and “Ns” is the total number of testees who belong to this institution.
The institutions are ranked according to their TWS. If there is a tie, the institutions are supposed to have the same rank, and they shall be printed in ascending order of Ns. If there is still a tie, they shall be printed in alphabetical order of their codes.
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

----------------

解题思路:

  建立一个Node结构体,包含输出结果中的各种元素,根据题目要求定义好自己的cmp函数,用unordered_map 定义输入里面的每组数据,统计总分数和总人数,输出的时候,定义lastscore 用于判断当前的分数和上次的分数是否像相同,(注意:初始化的lastscore不能是可能的分数,包括零,这里初始为-1;)如果不相同,则序号为当前下标+1。

对于分数的统计,过程要用double存储,最后转换成整形。

#include<vector>
#include<iostream>
#include<map>
#include<unordered_map>
#include<algorithm>
#include<string>
#pragma warning(disable:4996)
using namespace std;
int n;
struct Node {
	string school;
	int ts, tp;
};
bool cmp(Node &a, Node  &b) {
	if (a.ts != b.ts)return a.ts > b.ts;
	else if (a.tp != b.tp) return a.tp < b.tp;
	else
		return a.school < b.school;
}
int main()
{
	cin >> n;
	unordered_map<string, int>cnt;
	unordered_map<string, double>sum;//int 改成了double
	for (int i = 0; i < n; i++) {
		string id, school;
		double score;
		cin >> id;
			scanf("%lf", &score);
			cin>> school;
		for (int j = 0; j < school.size(); j++) {
			school[j] = tolower(school[j]);
		}
		if (id[0] == ‘B‘)
			score = score / 1.5;
		else if (id[0] == ‘T‘)
			score = score * 1.5;
		cnt[school]++;
		sum[school] += score;
	}
	vector<Node> ans;
	for (auto i = cnt.begin(); i != cnt.end();i++)
	{
		ans.push_back(Node{ i->first,(int)sum[i->first],cnt[i->first] });// sum前加int强制类型转换
	}
	printf("%d
", ans.size());
	sort(ans.begin(),ans.end(),cmp);
	int tmp = 0;
	int lastscore = -1;
	/*for (auto i = ans.begin(); i != ans.end();i++) {
		if (0 == lastscore || (*i).ts != lastscore)lastscore = (*i).ts, tmp = i+1;
		printf("%d %s %d %d
", tmp, (*i).school.c_str(),(*i).ts,(*i).tp);
	}*/
	for (int i = 0; i < ans.size(); i++)
	{
		if ( ans[i].ts != lastscore) tmp = i + 1;
		lastscore = ans[i].ts;
		printf("%d %s %d %d
", tmp, ans[i].school.c_str(), ans[i].ts, ans[i].tp);
	}
	getchar();
	getchar();
	return 0;
}

  

以上是关于1141 PAT Ranking of Institutions PAT甲级的主要内容,如果未能解决你的问题,请参考以下文章

1141 PAT Ranking of Institutions (25)

PAT甲级1141PAT Ranking of Institutions

A 1141 PAT Ranking of Institutions (25分)

1141 PAT Ranking of Institutions (25 分)难度: 一般 / 知识点: 排序

PAT甲级——A1025 PAT Ranking

PAT A1025 PAT Ranking(25)