Skip to main content

1025PATRanking排序

·447 words·3 mins
WFUing
Author
WFUing
A graduate who loves coding.
Table of Contents

1025 PAT Ranking
#

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.

1、大致题意
#

n 个考场,每个考场有若干数量的学生,给出每个考场中考生的编号和分数,要求算排名,输出所有考生的编号、排名、考场号、考场内排名

2、基本思路
#

简单题

3、解题过程
#

3.1 坑点 - 测试点4(22/25)
#

ID有类似 000003 这种形式的,如果是数字类型就容易输出错误,前导零别忘了。

#include<iostream>
#include<algorithm>
int n,k[105],cnt;
struct Student {
	long long num;
	int score;
	int final_rank,location_number,local_rank;
	bool operator >(const Student &a)const {
		if(score!=a.score) {
			return score>a.score;
		} else {
			return num<a.num;
		}
	};
} stu[10000005];
using namespace std;
int main() {
	cin>>n;
	cnt=0;
	for(int i=1; i<=n; i++) {
		cin>>k[i];
		for(int j=1; j<=k[i]; j++) {
			cin>>stu[cnt+j].num>>stu[cnt+j].score;
		}
		sort(stu+cnt+1,stu+cnt+k[i]+1,greater<Student>());
		stu[cnt+1].location_number=i;
		stu[cnt+1].local_rank=1;
		for(int j=2; j<=k[i]; j++) {
			if(stu[cnt+j].score==stu[cnt+j-1].score) {
				stu[cnt+j].local_rank=stu[cnt+j-1].local_rank;
			} else {
				stu[cnt+j].local_rank=j;
			}
			stu[cnt+j].location_number=i;
		}
		cnt+=k[i];
	}
	sort(stu+1,stu+cnt+1,greater<Student>());
	stu[1].final_rank=1;
	for(int i=2; i<=cnt; i++) {
		if(stu[i].score==stu[i-1].score) {
			stu[i].final_rank=stu[i-1].final_rank;
		} else {
			stu[i].final_rank=i;
		}
	}
	cout<<cnt<<endl;
//	1234567890005 1 1 1
	for(int i=1; i<=cnt; i++) {
		cout<<stu[i].num<<" "<<stu[i].final_rank<<" "<<stu[i].location_number<<" "<<stu[i].local_rank<<endl;
	}
	return 0;
}

./figures/93933006d8c241e08612f90104ea8a38.png

3.2 AC代码
#

#include<iostream>
#include<algorithm>
#include<iomanip>
int n,k[105],cnt;
struct Student {
	long long num;
	int score;
	int final_rank,location_number,local_rank;
	bool operator >(const Student &a)const {
		if(score!=a.score) {
			return score>a.score;
		} else {
			return num<a.num;
		}
	};
} stu[10000005];
using namespace std;
int main() {
	cin>>n;
	cnt=0;
	for(int i=1; i<=n; i++) {
		cin>>k[i];
		for(int j=1; j<=k[i]; j++) {
			cin>>stu[cnt+j].num>>stu[cnt+j].score;
		}
		sort(stu+cnt+1,stu+cnt+k[i]+1,greater<Student>());
		stu[cnt+1].location_number=i;
		stu[cnt+1].local_rank=1;
		for(int j=2; j<=k[i]; j++) {
			if(stu[cnt+j].score==stu[cnt+j-1].score) {
				stu[cnt+j].local_rank=stu[cnt+j-1].local_rank;
			} else {
				stu[cnt+j].local_rank=j;
			}
			stu[cnt+j].location_number=i;
		}
		cnt+=k[i];
	}
	sort(stu+1,stu+cnt+1,greater<Student>());
	stu[1].final_rank=1;
	for(int i=2; i<=cnt; i++) {
		if(stu[i].score==stu[i-1].score) {
			stu[i].final_rank=stu[i-1].final_rank;
		} else {
			stu[i].final_rank=i;
		}
	}
	cout<<cnt<<endl;
	for(int i=1; i<=cnt; i++) {
		cout<<setfill('0')<<setw(13)<<stu[i].num<<" "<<stu[i].final_rank<<" "<<stu[i].location_number<<" "<<stu[i].local_rank<<endl;
	}
	return 0;
}

./figures/2f69b0d0c6644fe6b6c370bfcccad2b3.png