PAT-1012 The Best Rank

/*PAT-1012 The Best Rank*/
/*本题考察结构体、排序算法sort的运用,如果用STL库vector可简化计算。*/
/*可能遇到的问题是,排序的分数如果相同,那么他们的排名相同,后面的人排名向后移。
比如: 第一名和第二名的成绩相同,那么他们的排名都是1,而第三名成绩是3.*/
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

typedef struct{
	string id;
	int c;
	int m;
	int e;
	int a;
	int rank;
	char subject;
}record;

bool sort_a(record re1, record re2)
{
	return re1.a > re2.a;
}
bool sort_c(record re1, record re2)
{
	return re1.c > re2.c;
}
bool sort_m(record re1, record re2)
{
	return re1.m > re2.m;
}
bool sort_e(record re1, record re2)
{
	return re1.e > re2.e;
}
void print(vector<record>& re){
	for(int i = 0; i < re.size(); ++i)
	{
		cout << re[i].id << endl;
	}
}
void getRank(vector<record>& re, vector<int>& rank, char sub){
	int val = 1;
	for(int i = 0; i < re.size(); ++i)
	{
		if(i == 0){
			rank[i] = 1;
		}
		else{
			if(sub == 'A' && re[i].a == re[i-1].a){
				rank[i] = rank[i-1];
			}
			else if(sub == 'A' && re[i].a != re[i-1].a){
				rank[i] = i+1;
			}
			else if(sub == 'C' && re[i].c == re[i-1].c){
				rank[i] = rank[i-1];
			}
			else if(sub == 'C' && re[i].c != re[i-1].c){
				rank[i] = i+1;
			}
			else if(sub == 'M' && re[i].m == re[i-1].m){
				rank[i] = rank[i-1];
			}
			else if(sub == 'M' && re[i].m != re[i-1].m){
				rank[i] = i+1;
			}
			else if(sub == 'E' && re[i].e == re[i-1].e){
				rank[i] = rank[i-1];
			}
			else if(sub == 'E' && re[i].e != re[i-1].e){
				rank[i] = i+1;
			}
		}
	}
}

int main()
{
	vector<record> scores;
	vector<string> ids;
	
	int n,m;
	
	cin >> n >> m;
	vector<int> rank(n);
	
	for(int i = 0; i < n; ++i)
	{
		record score;
		cin >> score.id >> score.c >> score.m >> score.e;
		int as = (score.c + score.m + score.e + 1) / 3;
		score.a = as;
		scores.push_back(score);
	}
	string id;
	for(int i = 0; i < m; ++i)
	{
		cin >> id;
		ids.push_back(id);
	}

	//cout << "按平均分排序:" << endl;

	rank.assign(n,0);
	sort(scores.begin(), scores.end(), sort_a);
	getRank(scores, rank, 'A');
	for(int i = 0; i < n; ++i)
	{
		scores[i].rank = rank[i];
		scores[i].subject = 'A';	
	}
	//cout << "按C排序:" << endl;
	//print(scores);
	rank.assign(n,0);
	sort(scores.begin(), scores.end(), sort_c);
	getRank(scores, rank, 'C');
	for(int i = 0; i < n; ++i)
	{
		if(rank[i] < scores[i].rank)
		{
			scores[i].rank = rank[i];
			scores[i].subject = 'C';	
		}
	}
	//cout << "按数学排序:" << endl;
	rank.assign(n,0);
	sort(scores.begin(), scores.end(), sort_m);
	getRank(scores, rank, 'M');
	for(int i = 0; i < n; ++i)
	{
		if(rank[i] < scores[i].rank)
		{
			scores[i].rank = rank[i];
			scores[i].subject = 'M';	
		}
	}
	//cout << "按英语排序:" << endl;

	rank.assign(n,0);
	sort(scores.begin(), scores.end(), sort_e);
	getRank(scores, rank, 'M');
	for(int i = 0; i < n; ++i)
	{
		if(rank[i] < scores[i].rank)
		{
			scores[i].rank = rank[i];
			scores[i].subject = 'E';	
		}
	}
	
	for(int i = 0; i < m; ++i)
	{
		int j;
		for(j = 0; j < n; ++j)
		{
			if(scores[j].id == ids[i])
			{
				cout << scores[j].rank << " " << scores[j].subject << endl;
				break;
			}
		}
		if(j >= n)
		{
			cout << "N/A" << endl;
		}
	}
	return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值