首页 > 其他 > 详细

PAT(甲级) -- 1025 PAT Ranking

时间:2020-07-15 20:21:16      阅读:45      评论:0      收藏:0      [点我收藏+]

题目

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;
}

PAT(甲级) -- 1025 PAT Ranking

原文:https://www.cnblogs.com/K2MnO4/p/13306998.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!