【PAT】1025. PAT Ranking (25)

1025. PAT Ranking (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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
方法一 cmp sort()

#include <stdio.h>
#include<iostream>
#include <string.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
//C语言的qsort与c++语言的sort 
//由于qsort需要使用指针,而且写法上也没有C++的sort简单,
//所以以后使用C++的sort来解决排序问题
//下面解决PAT  A1025 
struct Student{
	char id[15];
	int s;
	int l;
	int lr;//排名rank 
}stu[30010]; 
//由于使用sort排名需要提供bool cmp()函数,所以下面定义cmp函数
bool cmp(Student a,Student b){
	if(a.s!=b.s) return a.s>b.s;//从大到小a>b,从小到大a<b 
	else return strcmp(a.id,b.id)<0;//strcmp是string.h头文件下用来比较两个char型字符串的字典序大小,
	//str1小于str2返回负数 
}
int main(){
	//sort(首位元素地址,末尾元素地址加一,cmp) 
	int n,k,num=0;
	scanf("%d",&n);
	for(int i=1;i<=n;i++){
		scanf("%d",&k);
		for(int j=0;k<k;j++){
			scanf("%s %d",stu[num].id,&stu[num].s);
			stu[num].l=i;
			num++;
		}
		sort(stu+num-k,stu+num,cmp);
		stu[num-k].lr=1;
		for(int j=num-k+1;j<num;j++){
			if(stu[j].s==stu[j-1].s){
				stu[j].lr=stu[j-1].lr;
			}else{
				stu[j].lr=j+1-(num-k);
			}
		}
	}
	printf("%d\n",num);
	sort(stu,stu+num,cmp);
	int r=1;
	for(int i=0;i<num;i++){
		if(i>0&&stu[i].s!=stu[i-1].s){
			r=i+1;
		}
		printf("%s ",stu[i].id);
		printf("%d %d %d\n",r,stu[i].l,stu[i].lr);
	}
	return 0;
}

方法二:

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<functional>
using namespace std;
typedef struct student
{
	string num;
	int grade;
	int final_rank;
	int location_num;
	int local_rank;
	//注意以下这段的写法
	bool operator > (const student& s)const
	{
		if(grade != s.grade)
			return grade > s.grade;
		else return num < s.num;
	}
}student;

#define max 30000
#define INF 0x6FFFFFFF
vector<student> v;

int main()
{
	int n,m;
	int i,j;
	cin>>n;
	for(i=0; i<n; i++)
	{
		cin>>m;
		vector<student> temp(m);
		for(j=0; j<m; j++)
		{
			cin>>temp[j].num>>temp[j].grade;
			temp[j].location_num = i+1;
		}
		sort(temp.begin(),temp.end(),greater<student>());
		int nowGrade = INF;
		int nowRank = 0;
		for(j=0; j<m; j++)
		{
			
			//cout<<temp[j].num<<"   "<<temp[j].location_num<<endl;
			if(temp[j].grade == nowGrade)
				temp[j].local_rank = nowRank;
			else
			{
				temp[j].local_rank = j+1;
				nowRank = j + 1;
				nowGrade = temp[j].grade;
			}
			v.push_back(temp[j]);
		}
	}
	// 处理global_rank
	sort(v.begin(),v.end(),greater<student>());   //此处用到greater,头文件应该有#include<functional>
	int nowGrade = INF;
	int nowRank = 0;
	cout<<v.size()<<endl;
	for(i=0; i<v.size(); i++)
	{
		if(v[i].grade == nowGrade)
			v[i].final_rank = nowRank;
		else 
		{
			nowRank = i+1; 
			v[i].final_rank = i+1;
			nowGrade = v[i].grade;
		}
		cout<<v[i].num<<" "<<v[i].final_rank<<" "<<v[i].location_num<<" "<<v[i].local_rank<<endl;
	}

	return 0;
}


方法三:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

struct node{
	string id;
	int localId, localRank, rank;
	int gra;
};

vector<node> all;

bool cmpAll(node a, node b){
	if(a.gra != b.gra){
		return a.gra > b.gra;
	}else{
		return a.id < b.id;
	}
}

bool cmpLocal(node a, node b){
	if(a.localId != b.localId){
		return a.localId < b.localId;	
	}else{
		return a.gra > b.gra;
	}
	   
}

int main(int argc, char** argv) {
	int n,i, num, gra;
	string id;
	scanf("%d",&n);
	for(i=0; i<n; i++){
		scanf("%d",&num);
		for(int j=0; j<num; j++){
			node tmp;
			cin>>tmp.id>>tmp.gra;
			tmp.localId = i+1;
			all.push_back(tmp);	 
		}
	}

	int localId = all[0].localId, localRank = 1;
	int localCnt = 0;
	sort(all.begin(), all.end(), cmpLocal);
	for(i=0; i<all.size(); i++){
		if(i==0){
			all[i].localRank = 1;
			localCnt = 1;
		}else{
			if(all[i].localId != localId){
				localCnt = 1;
				localRank = 1;
				all[i].localRank = 1;
				localId = all[i].localId;
			}else{
				localCnt++;
				if(all[i].gra == all[i-1].gra){
					all[i].localRank = localRank;
				}else{
					all[i].localRank = localCnt;
					localRank = localCnt;
				}				
			}
		}
	}	
	
	int rank=1;	
	sort(all.begin(), all.end(), cmpAll);
	for(i=0; i<all.size(); i++){
		if(i==0){
			all[i].rank = rank;
		}else{
			if(all[i].gra == all[i-1].gra){
				all[i].rank = rank;
			}else{
				all[i].rank = i+1;
				rank = i+1;
			}			
		}
	}	
	cout<<all.size()<<endl;
	for(i=0; i<all.size(); i++){
		cout<<all[i].id<<" ";
		printf("%d %d %d\n",all[i].rank, all[i].localId, all[i].localRank);
	}	
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值