PAT 1141 PAT Ranking of Institutions (25point(s))(刷题记录)

  • This problem gives a list of PAT testees’ information including their ID,Score and school.We have to collect the institutions where they from,and output the ranklist of institutions in nondecreasing order of their ranks.
  • My Idea
    Define a struct insti to store the school name,its testees ’ scores(B,A,T),and the number of its testees.Initialize an array I of insti type to store these institutions.For convenience,I used a map to help us find the index of school in array I quickly.Then I write a comparision function to make it easier to get the ranklist of institutions.
  • Something need to be NOTED
    1.The string of school name has to be in lower case
    2.The total weighted score which is defined to be the integer part of ScoreB/1.5 + ScoreA + ScoreT*1.5

Here’s my code.

#include <iostream>
#include <stdio.h>
#include<stdlib.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include<queue>
#include <unordered_map>
#include <set>

using namespace std;

const int maxn = 100100;
typedef struct insti {
	string school;
	int sB, sA, sT;
	int Ns;
};
insti I[maxn];
map<string, int> index;

bool cmp(insti A, insti B)
{
	int s1 = int(A.sB / 1.5 + A.sA + A.sT*1.5);
	int s2 = int(B.sB / 1.5 + B.sA + B.sT*1.5);
	
	if (s1 != s2) return s1 > s2;
	else if (A.Ns != B.Ns) return A.Ns < B.Ns;
	else return A.school < B.school;
}

int main() {
	int N;
	cin >> N;
	string ID, inst;
	int score;
	for (int i = 0; i < N; i++)
	{
		cin >> ID >> score >> inst;
		transform(inst.begin(), inst.end(), inst.begin(), ::tolower);
		if (!index.count(inst))
		{
			int ind=index.size();
			index[inst] = ind;
			I[ind].school = inst;
			I[ind].sA = 0; I[ind].sB = 0; I[ind].sT = 0;
			if (ID[0] == 'B') I[ind].sB = score;
			else if (ID[0] == 'A') I[ind].sA = score;
			else I[ind].sT = score;
			I[ind].Ns = 1;
		}
		else {
			++I[index[inst]].Ns;
			if (ID[0] == 'B')I[index[inst]].sB += score;
			else if (ID[0] == 'A') I[index[inst]].sA += score;
			else I[index[inst]].sT += score;
		}
		

	}
	sort(I, I + index.size(), cmp);
	
	cout << index.size() << endl;
	
	int rank = 1;
	int cnt = 1; //平局的学校数
	cout << 1 << " " << I[0].school
		<< " " << int(I[0].sB / 1.5 + I[0].sA + I[0].sT*1.5)
		<< " " << I[0].Ns << endl;
	for (int i = 1; i < index.size(); i++) {
		if (int(I[i].sB / 1.5 + I[i].sA + I[i].sT*1.5) == int(I[i - 1].sB / 1.5 + I[i - 1].sA + I[i - 1].sT*1.5))
		{
			++cnt;
			cout << rank << " " << I[i].school
				<< " " << int(I[i].sB / 1.5 + I[i].sA + I[i].sT*1.5)
				<<" "<< I[i].Ns << endl;
		}
		else {
			rank += cnt;
			cnt = 1;
			cout << rank << " " << I[i].school
				<< " " << int(I[i].sB / 1.5 + I[i].sA +I[i].sT*1.5)
				<<" "<< I[i].Ns << endl;
		}
	}

	return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值