PAT A1025【 PAT Ranking】

15 篇文章 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

 

算法思路:

第一步:定义一个testee结构体,内置学号、分数、考场数、考场排名、最终排名;利用一维数组来存储所有的考生

第二步:读取考场数

第二步:根据考场数量,用for循环来读取每个考场的人数和考生信息

                在一个for循环内做的内容有:读取该考场考生数;

                内置一个for循环来读取一个考场考生的信息: 学号,分数,考场

                对该考场的考生成绩进行排序,得到localRank

第三步:对所有考生进行排序,得到finalRank

第四步:按要求格式输出

代码展示:

#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
//定义结构体
struct testee{
	char rgsNum[13];
	int sorce;
	int local;
	int localRank;
	int finalRank;
}test[101*301];   //定义数组,存储输入的考生
//定义排序函数
bool cmp(testee a,testee b){
	if(a.sorce != b.sorce)
		return a.sorce>b.sorce;
	else
		return strcmp(a.rgsNum,b.rgsNum)<0;
}
//主函数
int main(){
	int locals;
	scanf("%d",&locals);
	int lastLocal = 0;//记录当前考场考生在数组中的下标;
	for (int i=1;i<=locals;i++){
		int localNum;
		scanf("%d",&localNum);
		for (int j=lastLocal;j<lastLocal+localNum;j++){
			scanf("%s %d",&test[j].rgsNum,&test[j].sorce);
			test[j].local=i;  
		}
		//对单个考场成绩进行排序;范围为lastLocal到lastLocal+localNum(当前考场人数)
		sort(test+lastLocal,test+lastLocal+localNum,cmp);
		for(int j=lastLocal,rank=1;j<localNum+lastLocal;j++){
			if(j>0 && test[j].sorce < test[j-1].sorce){
				rank=j+1-lastLocal;
			}
			test[j].localRank = rank;
		}
		lastLocal += localNum;
	}
    //对所有考生进行排序,lastLocal可以作为考生总人数
	sort(test,test+lastLocal,cmp);
	int rank=1;
	for(int j=0;j<lastLocal;j++){
			if(j>0 && test[j].sorce < test[j-1].sorce){
				rank=j+1;
			}
			test[j].finalRank = rank;
	}
    //结果输出
	printf("%d\n",lastLocal);
	for(int j=0;j<lastLocal;j++){
		printf("%s %d %d %d\n",test[j].rgsNum,test[j].finalRank,test[j].local,test[j].localRank);
	}
	return 0;

注意: 

1、sort(start,end,cmp)

函数中start和end是指针类型,所以应写sort(a,a+len,cmp);而不是sort(a[strat],a[end],cmp).

2、排序我们直接调用c++库函数中提供的排序函数sort(start,end,cmp)

比较函数:

        当分数不同时,直接按分数排序;当分数相同时,按照学号排序;

       我们知道,对纯数字组成的字符串来说,字符串比较结果和字符串对应的数字比较结果相同,所以直接使用     strcmp(str1,str2)<0;

bool cmp(testee a,testee b){
	if(a.sorce != b.sorce)
		return a.sorce>b.sorce;
	else
		return strcmp(a.rgsNum,b.rgsNum)<0;
}

 转载请声明!!!

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值