1. 程式人生 > 實用技巧 >PAT(甲級) -- 1025 PAT Ranking

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. 建議使用字串來儲存准考證號,資料有前導0,開LL的話,不注意可能不能AC.
  2. 假如有A,B,C三者,A與B總成績相同,則二者排名均為1,但是C的排名不能為2,而是為3.
  3. 總成績可能為0,要進行特判.

AC程式碼:

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<math.h>
using namespace std;

struct student
{
	char id[15];
	int grade, grade_rank, group, group_rank;
}a[30005];

bool cmp(student b, student c)
{
	if(b.grade != c.grade)
		return b.grade > c.grade;
	else
		return strcmp(b.id, c.id) < 0;
}

int main()
{
	int t, n;
	scanf("%d", &t);
	int cnt = 0;
	for(int i = 1; i <= t; i++)
	{
		scanf("%d", &n);
		for(int j = 1; j <= n; j++)
		{
			cnt ++;
			scanf("%s%d", a[cnt].id, &a[cnt].grade);
			a[cnt].group = i; //組別 
		}
	}
	sort(a + 1, a + 1 + cnt, cmp);
	for(int i = 1; i <= cnt; i++)
	{
		if(a[i].grade == a[i-1].grade && i != 1) //如果前後兩個考生總成績相同 ,要進行特判 
			a[i].grade_rank = a[i-1].grade_rank; //該考生的成績排名和上一個考生一樣 
		else
			a[i].grade_rank = i; //否則按順序往下 
	}
	for(int i = 1; i <= t; i++)
	{
		int num = 0, lastPos = 0; //每一組學生個數; 上一次同一組學生的下標 
		for(int j = 1; j <= cnt; j++)
		{
			if(a[j].group == i) 
			{
				if(a[j].grade == a[lastPos].grade && j != 1) //總成績可能為0, 特判 
				{
					a[j].group_rank = a[lastPos].group_rank; //否則和同一組的上一個考生組排名一致 
					lastPos = j;
					num ++;
					
				}
				else
				{
					num ++;
					a[j].group_rank = num; //該考生所在的組別的的組排名 
					lastPos = j;
				}
			}
		}
	}
	printf("%d\n", cnt);
	for(int i = 1; i <= cnt; i++)
		printf("%s %d %d %d\n", a[i].id, a[i].grade_rank, a[i].group, a[i].group_rank);
	return 0;
}