PAT刷题记录 1025 PAT Ranking (25分)

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


/*
	总结:
	用vector的insert来进行合并操作
*/

struct Student {
	string id;
	int grade;
	int location_number;
	int local_rank;
	int final_rank;
};
vector<Student> stu[105];

int now;
void local_rank() {
	stu[now][0].local_rank = 1;
	int size = stu[now].size();
	for (int i = 1; i < size; i++) {
		if (stu[now][i].grade == stu[now][i - 1].grade)
			stu[now][i].local_rank = stu[now][i - 1].local_rank;
		else stu[now][i].local_rank = i + 1;
	}
}

void final_rank() {
	stu[0][0].final_rank = 1;
	int size = stu[0].size();
	for (int i = 1; i < size; i++) {
		if (stu[0][i].grade == stu[0][i - 1].grade)
			stu[0][i].final_rank = stu[0][i - 1].final_rank;
		else stu[0][i].final_rank = i + 1;
	}
}


bool cmp(Student a, Student b) {
	return a.grade > b.grade;
}

bool cmp2(Student a, Student b) {
	if (a.grade != b.grade)return a.grade > b.grade;
	else return a.id < b.id;
}


int main() {
	int list_num;
	cin >> list_num;
	int cnt = 0;
	for (int i = 0; i < list_num; i++) {
		int n;
		cin >> n;
		cnt += n;
		for (int j = 0; j < n; j++) {
			Student temp;
			cin >> temp.id>>temp.grade;
			temp.location_number = i+1;
			stu[i].push_back(temp);
		}
		sort(stu[i].begin(), stu[i].end(), cmp);
		now = i;
		local_rank();
	}
	//合并rank
	for (int i = 1; i < list_num; i++) {
		stu[0].insert(stu[0].end(), stu[i].begin(), stu[i].end());
	}
	sort(stu[0].begin(), stu[0].end(), cmp2);
	final_rank();
	printf("%d\n", cnt);
	for (int i = 0; i < cnt; i++) {
		cout << stu[0][i].id;
		printf(" %d %d %d\n", stu[0][i].final_rank, stu[0][i].location_number, stu[0][i].local_rank);
	}
	system("pause");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值