PAT 1025

题目

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,在分别给出每个考场的人数和学生数据。所给的学生数据包括学号,成绩,要求按照总排名的顺序输出,每个人的总排名考场号,并且输出学生在考场的排名。

代码和思路

思路

  1. 题给学生数据为准考证号,成绩,班级,要求输出有总排名,和考场排名。故定义一结构体,包含上述五个部分。为方便输入,在结构体内定义构造函数。
  2. 学号长度长,直接用string就可以。
  3. 排序的第一关键字是分数,为降序排列;第二关键字为id,未不降序排列。
  4. 不需要总排名分排名各排序一遍,在进行总排序的时候,对每个考场的学生进行记录。记录该考场已经排序的人数,用于确定学生的名次;记录上一个学生在总学生中的位置,用于在总排名中进行成绩比对;记录一个考场上一个学生的位置,用于在考场排名中进行比对。
  5. 不知道有多少个学生,用vector比较好一些。这样不用自己估计学生人数也不用考虑段错误问题。

代码

#include
#include
#include
#include
#include
using namespace std;

struct student {
	string id;
	int score;
	int local_rank = 0;
	int final_rank = 0;
	int local;
	student (string _str, int _score, int _local):id(_str), score(_score), local(_local) {}
};

bool cmp(student a, student b) {
	if (a.score != b.score) {
		return a.score > b.score;
	}
	else if(a.id != b.id) {
		return a.id < b.id;
    }
}

vector<student> stu;

void getRank() {
	int final_rank = 0; //记录总排名的人数
	int local_rank[110] = { 0 }; //记录每个班被排名的人数
	int local[110]; //记录每个班前一个人的人位置;
	fill(local, local + 100, -1);
	for (int i = 0; i < stu.size(); i++) {
		local_rank[stu[i].local]++;
		final_rank++;
		if (local[stu[i].local] == -1) {
			stu[i].local_rank = local_rank[stu[i].local];
		}
		else {
			if (stu[i].score == stu[local[stu[i].local]].score) {
				stu[i].local_rank = stu[local[stu[i].local]].local_rank;
			}
			else {
				stu[i].local_rank = local_rank[stu[i].local];
			}
		}
		local[stu[i].local] = i;
		if (i == 0) {
			stu[i].final_rank = final_rank;
		}
		else {
			if (stu[i].score == stu[i - 1].score) {
				stu[i].final_rank = stu[i - 1].final_rank;
			}
			else {
				stu[i].final_rank = final_rank;
			}
		}
	}
}

int main() {
	int n, k;
	string str;
	int temp;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &k);
		for (int j = 0; j < k; j++) {
			cin >> str;
			scanf("%d", &temp);
			stu.push_back(student(str, temp, i + 1));
		}
	}
	sort(stu.begin(), stu.end(), cmp);
	getRank();
	temp = stu.size();
	printf("%d\n", temp);
	for (int i = 0; i < stu.size(); i++) {
		cout << stu[i].id << " " ;
		printf("%d %d %d\n", stu[i].final_rank, stu[i].local, stu[i].local_rank);
	}
	return 0;
}

小结

这个题目还是对sort函数和cmp函数的使用,排序问题在不考虑自写排序算法的情况下难度都不高,搞清楚关键字和排序的对象还有读入输出问题就可以了。

你可能感兴趣的:(PAT)