PAT甲级 A1025

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

题目大意:输入考场数N,以及每个考场人数为K,相印给出K个人的考号与成绩。

输出总考生人数,考号,全排名,考场号,考场排名 

最初的自己的代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node
{
	char id[15];
	int score;//成绩
	int num;//考场号
	int rank1;//考场排名
	int rank2;//考生总排名
}stu[30010];
bool cmp(node a, node b)
{
	if (a.score != b.score)
	{
		return a.score > b.score;
	}
	else
	{
		return strcmp(a.id, b.id) < 0;
	}
}
int main()
{
	int n;//考场数
	int m, k = 0;//m=每个考场人数,k记录所有人数
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &m);
		for (int j = 0; j < m; j++)
		{
			scanf("%s%d", stu[k].id, &stu[k].score);
			stu[k].num = i;
			k++;
		}
		sort(stu + (k - m), stu + k, cmp);
		stu[k - m].rank1 = 1;//----------------------------排名
		int r1 = 1;//排名
		for (int a = k - m + 1; a < k; a++)
		{
			r1++;
			if (stu[a].score == stu[a - 1].score)
			{
				stu[a].rank1 = stu[a - 1].rank1;
			}
			else
			{
				stu[a].rank1 = r1;
			}
		}
	}
	sort(stu, stu + k, cmp);
	stu[0].rank2 = 1;//-------------------------排名
	int r = 1;
	for (int i = 1; i < k; i++)
	{
		r++;
		if (stu[i].score == stu[i - 1].score)
		{
			stu[i].rank2 = stu[i - 1].rank2;
		}
		else
		{
			stu[i].rank2 = r;
		}
	}
	printf("%d\n", k);
	for (int i = 0; i < k; i++)
	{
		printf("%s ", stu[i].id);
		printf("%d %d %d", stu[i].rank2, stu[i].num, stu[i].rank1);
		if (i<k - 1)
		{
			printf("\n");
		}
	}
	return 0;
}

 根据算法笔记修改删减一些代码

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Student
{
	char id[15];//考号
	int score;//成绩
	int local_number;//考场号
	int local_rank;//地区排名
}Stu[30010];
bool cmp(Student a, Student b)
{
	if (a.score != b.score)
	{
		return a.score > b.score;
	}
	else
	{
		return strcmp(a.id, b.id) < 0;
	}
}
int main()
{
	int n, k, num = 0;
	scanf("%d", &n);
	for (int i =1 ; i <= n; i++)//第几号考场
	{
		scanf("%d", &k);
		for (int j = 0; j < k; j++)
		{
			scanf("%s %d", Stu[num].id, &Stu[num].score);//成绩
			Stu[num].local_number = i;//考场号
			num++;//人数
		}
		sort(Stu+num-k, Stu+num, cmp);//考场排序
		Stu[num - k].local_rank = 1;
		for (int j = num - k + 1; j < num; j++)
		{
			if (Stu[j].score == Stu[j -1].score)
			{
				Stu[j].local_rank = Stu[j - 1].local_rank;
			}
			else
			{
				Stu[j].local_rank = j + 1 - (num - k);//剩余排名
			}
		}
	}
	printf("%d\n", num);
	sort(Stu, Stu+num, cmp);
	int m = 1;
	for (int i = 0; i < num; i++)
	{
		if (i>0&&Stu[i].score != Stu[i - 1].score)
		{
			m = i + 1;
		}
		printf("%s ",Stu[i].id);
		printf("%d %d %d\n",m,Stu[i].local_number, Stu[i].local_rank);
	}
	return 0;
}
发布了9 篇原创文章 · 获赞 10 · 访问量 148

猜你喜欢

转载自blog.csdn.net/VictorierJwr/article/details/103976076