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\n", 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\n", 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\n", tmp, ans[i].school.c_str(), ans[i].ts, ans[i].tp);
	}
	getchar();
	getchar();
	return 0;
}

  

原文地址:https://www.cnblogs.com/zxzmnh/p/11654982.html

时间: 2024-08-30 17:21:13

1141 PAT Ranking of Institutions PAT甲级的相关文章

1141 PAT Ranking of Institutions[难]

1141 PAT Ranking of Institutions (25 分) 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

1025. PAT Ranking

1025. PAT Ranking (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneous

pat1025. PAT Ranking (25)

1025. PAT Ranking (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneous

1025 PAT Ranking[排序][一般]

1025 PAT Ranking (25)(25 分) Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediat

1025 PAT Ranking (25 分)

1025 PAT Ranking (25 分) Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately

1025 PAT Ranking (25分)

1025 PAT Ranking (25分) 1. 题目 2. 思路 设置结构体, 先对每一个local排序,再整合后排序 3. 注意点 整体排序时注意如果分数相同的情况下还要按照编号排序 4. 代码 #include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; struct stu{ int location_number; char

PAT Advanced 1093 Count PAT&#39;s (25分)

The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th characters. Now given any string, you are supposed to tell the numb

PAT甲级——A1025 PAT Ranking

Programming Ability Test (PAT) is organized by the College of Computer Science and Technology of Zhejiang University. Each test is supposed to run simultaneously in several places, and the ranklists will be merged immediately after the test. Now it i

PAT 甲级1025 PAT Ranking (25 分)(结构体排序,第一次超时了,一次sort即可小技巧优化)

题意: 给定一次PAT测试的成绩,要求输出考生的编号,总排名,考场编号以及考场排名. 分析: 题意很简单嘛,一开始上来就,一组组输入,一组组排序并记录组内排名,然后再来个总排序并算总排名,结果发现最后一个测试点超时. 发现自己一开始太傻太盲目,其实只要一次性全部输进来,记录好考场编号,一次排序就可以了.既然只排了一次,怎么计算考场排名呢,这里我用了三个数组 int g_rank[100];//记录各个考场当前排到的名次 (当前最后一个人的名次) int g_score[100];//记录个考场当