PAT-1025 PAT Ranking (25)

题目大意:给出 n 个PAT考点,在每个考点有相应数量的学生,输入学生考号和成绩。要求输出总榜,规则为:总榜成绩排名非递减,相同成绩报名递增输出;输出格式为:考生报名号 总榜排名  所在组别 所在组别排名。

解题思路:先计算考生在对应组别中的排名,然后归入总榜计算总排名。注意对排列的特殊要求即可。

题目链接:https://www.patest.cn/contests/pat-a-practise/1025

#include <iostream>              
#include <algorithm>              
#include <set>              
#include <map>              
#include <vector>              
#include <stack>              
#include <queue>              
#include <cmath>   
#include <cstring>           
using namespace std;
typedef struct Info
{
	char Num[15];
	int finalRank,localRank,location_num,grade; 
}Info;
Info stu[30005];

bool cmp(Info x1,Info x2)
{
	if(x1.grade > x2.grade)
		return true;
	else if(x1.grade == x2.grade)
	{
		if(strcmp(x1.Num,x2.Num) < 0)
			return true;
	}
	return false;
}

int main(int argc, char** argv) {
	int n;
	cin >> n;
	int g = 0;
	for(int i=0;i<n;++i)
	{
		int m;
		cin >> m;
		Info tmp[305];
		for(int j=0;j<m;++j)
		{	
			//cin >> tmp[j].localRank >> tmp[j].grade;
			scanf("%s %d",tmp[j].Num,&tmp[j].grade);
			tmp[j].location_num = i+1;
		}
		sort(tmp,tmp+m,cmp);
		tmp[0].localRank = 1;
		for(int j=1;j<m;++j)
		{
			if(tmp[j].grade == tmp[j-1].grade)
				tmp[j].localRank = tmp[j-1].localRank;
			else
				tmp[j].localRank = j+1;
		}
		for(int j=0;j<m;++j)
			stu[g++] = tmp[j];
	} 
	sort(stu,stu+g,cmp);
	stu[0].finalRank = 1;
	for(int i=1;i<g;++i)
	{
		if(stu[i].grade == stu[i-1].grade)
			stu[i].finalRank = stu[i-1].finalRank;
		else
			stu[i].finalRank = i+1;
	}
	cout << g << endl;
	for(int i=0;i<g;++i)
	{
		cout << stu[i].Num << " " << stu[i].finalRank << " " 
			 << stu[i].location_num << " " << stu[i].localRank 
			 << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/zhoujian_1943/article/details/79386114
今日推荐