PAT 甲级 1025 PAT Ranking

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.

输入格式

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.

输出格式

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.

输入样例

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

输出样例

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

题目大意

PAT考试有好几个考场,现在需要将所有考场的考生聚合在一个按成绩降序的列表中,这个列表中需要体现:

考号 最终排名(即在最终列表中的名次) 所在考场号 在考场中的排名

参考代码

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
typedef struct{
	//若为long long int,则存在前缀0问题,同时结构体内可以定义string 
	char num[13];//学号
	int list;//考场
	int score;//成绩
	int local_rank;//考场排名
}Info;
Info info[30001];
int N, local[101];
//自定义比较函数
bool cmp(Info x, Info y){
	if(x.score != y.score) return x.score > y.score;
	else return strcmp(x.num, y.num) < 0;
}
int main(){
	cin >> N;
	int i, j, num = 0, rank = 1;
	//N个考场
	for(i = 0; i < N; i++){
		int a, temp_score, start = num;
		scanf("%d", &a);
		//考场共a个人
		for(j = 0; j < a; j++){
			//赋考场号
			info[num].list = i + 1;
			//赋学号及得分
			scanf("%s%d", &info[num].num, &temp_score);
			info[num++].score = temp_score;
		}
		//用自定义比较函数sort
		sort(info + start, info + start + a, cmp);
		//第一名是rank1
		info[start].local_rank = 1;
		for(j = 2; j <= a; j++){
			//当前考生与上位考生得分相同,所以rank相同
			if(info[start + j - 1].score == info[start + j - 2].score) info[start + j - 1].local_rank = info[start + j - 2].local_rank;
			else info[start + j - 1].local_rank = j;
		}
	}
	sort(info, info + num, cmp);
	cout << num << endl; 
	cout << info[0].num << " " << 1 << " " << info[0].list << " " << info[0].local_rank << endl;
	for(i = 1; i < num; i++){
		if(info[i].score == info[i - 1].score){
			printf("%s %d %d %d\n", info[i].num, rank, info[i].list, info[i].local_rank);
		}else{
			printf("%s %d %d %d\n", info[i].num, i + 1, info[i].list, info[i].local_rank);
			rank = i + 1;
		}
	}
	return 0;
}

小结

PAT甲级的题目都是英文题面,一开始接触时难免有寸步难行之感。但实际上你并不需要知道题面中所有单词的含义,可以结合输入及输出样式推测出题目大致的意思,熟能生巧,长此以往便对英文题面没有那么抵触了。
这是一道比较典型的排序题,快来用相似例题练练手吧!
第三届传智杯C题
第三届传智杯C题题解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值