PAT甲级 1025 PAT Ranking (25分) (题目 + 代码 + 详细注释)

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组学生,要求对每组学生内部排序后再总体排序。主要考察结构体排序。变量有点多,注意细节即可

直接上我AC的代码吧:

#include<cstdio>
#include<algorithm>
#include<cstring>

using namespace std;
struct node { 
	char id[15];      //学号
	int score;        //成绩
	int f_rank;       //最终排名(final简写)
	int num;          //组号
	int l_rank;       //组内排名(local简写)
}stu[30005];        //由题意至少开到300 * 100

int 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, k, sum = 0;          //sum为当前已有的学生的总数,初始为0
	scanf("%d", &n);          //读入n
	for(int m = 1; m <= n; m++){         //m为组号
		scanf("%d", &k);            //读入k
		for (int j = 1; j <= k; j++) {
			scanf("%s%d", stu[sum].id, &stu[sum].score);       //注意:数组下标为sum(即所有学生连续存储)
			stu[sum].num = m;      //该学生的组号
			sum++;            //学生数+1
		}
		sort(stu + sum - k, stu + sum, cmp);     //对于该组的学生,下标范围[sum - k, sum),注意是左闭右开
		stu[sum - k].l_rank = 1;         //初始该组的第一学生的排名为1
		for (int i = sum - k + 1; i < sum; i++) {        
			if (stu[i].score == stu[i - 1].score)    //如果这名学生和上一名的学生分数一样,那么排名就一样
				stu[i].l_rank = stu[i - 1].l_rank;
			else
				stu[i].l_rank = i - (sum - k) + 1;      //否则组内排名就是组内编号(即组内下标 + 1)
		}
	}

	sort(stu, stu + sum, cmp);         //对全体学生排序
	stu[0].f_rank = 1;          //第一个学生的总排名为1
	for (int i = 1; i < sum; i++) {        //和上面的排名计算的原理一样,不再重复
		if (stu[i].score == stu[i - 1].score)
			stu[i].f_rank = stu[i - 1].f_rank;
		else
			stu[i].f_rank = i + 1;
	}
	printf("%d\n", sum);       //先输出总人数
	for (int i = 0; i < sum; i++)        //按题目要求输出学号,总排名,组号,组内排名
		printf("%s %d %d %d\n", stu[i].id, stu[i].f_rank, stu[i].num, stu[i].l_rank);

	return 0;
}

//本蒻蒻英语水平有限,若有翻译不对的地方,还望指正,谢谢!!

猜你喜欢

转载自blog.csdn.net/qq_45472866/article/details/104414040