PAT甲级 1025 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 is your job to write a program to correctly merge all the ranklists and generate the final rank.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive number N (≤100), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤300), the number of testees, and then K lines containing the registration number (a 13-digit number) and the total score of each testee. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in one line the total number of testees. Then print the final ranklist in the following format:

registration_number final_rank location_number local_rank

The locations are numbered from 1 to N. The output must be sorted in nondecreasing order of the final ranks. The testees with the same score must have the same rank, and the output must be sorted in nondecreasing order of their registration numbers.

Sample Input:

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

Sample Output:

9
1234567890005 1 1 1
1234567890014 1 2 1
1234567890001 3 1 2
1234567890003 3 1 2
1234567890004 5 1 4
1234567890012 5 2 2
1234567890002 7 1 5
1234567890013 8 2 3
1234567890011 9 2 4

解题思路:

  1. 题目要求对每个考场的成绩进行排名,再对于所有人进行排名。用结构体存储每个考生的信息及排名,每次先对输入当前考场的考生进行排名,最后再对所有考生进行排名。用num_people存储总人数,再用testee存储存储考场的人数,所以sort函数的开始位置:start = num_people - testee。结束位置:end = num_people。所以每次对考场中人数排名的sort函数为
sort(a+start,a+end,cmp);
  1. 对于sort函数中比较函数cmp的编写,题目要求先按照成绩从大到小进行排名,成绩相同则按照考生号从小到大进行排名。
bool cmp(struct PAT a,struct PAT b)
{
	if(a.grade!=b.grade)
		return a.grade > b.grade;
	else
		return strcmp(a.id,b.id) < 0;
}

代码

#include<cstdio>
#include<string.h>
#include<algorithm>
using namespace std;
struct PAT
	{
		char id[15];
		int grade;
		int rank;		 //总排名 
		int lo_rank;  	 //考点排名 
		int location;    //考点 
	}a[50000];
bool cmp(struct PAT a,struct PAT b)
{
	if(a.grade!=b.grade)
		return a.grade > b.grade;
	else
		return strcmp(a.id,b.id) < 0;
}

int main(void)
{
	int N;						//考点数 
	scanf("%d",&N);
	int start = 0;
	int end = 0;
	int sum_people = 0;
	for(int i = 1;i <= N;i++)
	{
		int testee;
		scanf("%d",&testee);					//考场的考生数 
		for(int j = 0;j < testee;j++)
		{
			scanf("%s %d",a[sum_people].id,&a[sum_people].grade);
			a[sum_people].location = i;
			sum_people++;
		}
		start = sum_people - testee;
		end = sum_people;
		sort(a+start,a+end,cmp);
		a[start].lo_rank = 1;
		for(int m = start+1,j = 2;m < end;m++,j++)
		{
			if(a[m].grade!=a[m-1].grade)
			{
				a[m].lo_rank = j;
			}
			else
			{
				a[m].lo_rank = a[m-1].lo_rank;
			}
		}
	}
	sort(a,a+end,cmp);
	a[0].rank = 1;
	for(int m = 1;m < end;m++)
	{
		if(a[m].grade!=a[m-1].grade)
		{
			a[m].rank = m + 1;
		}
		else
		{
			a[m].rank= a[m-1].rank;
		}
	}
	printf("%d\n",end);
	for(int i = 0;i < sum_people;i++)
	{
		printf("%s %d %d %d\n",a[i].id,a[i].rank,a[i].location,a[i].lo_rank);
	}
	return 0;	
}

对所有的考生排名可以采取以下直接输出的方式替换55-71行代码,避免先记录排名再输出,减少代码量。

int rank = 1;
	printf("%d\n",end);
	for(int i = 0;i < sum_people;i++)
	{
		if(i > 0&&a[i].grade!=a[i-1].grade)
			rank = i + 1;
		printf("%s %d %d %d\n",a[i].id,rank,a[i].location,a[i].lo_rank);
	}
发布了6 篇原创文章 · 获赞 1 · 访问量 209

猜你喜欢

转载自blog.csdn.net/lovingcgq/article/details/104112535
今日推荐