1025. PAT Ranking 解析

里面有测试成绩为0的情况。然后我的最小成绩设成0了,然后0的排序有问题。改成-1就好了。

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

struct Node {
	string id;
	int grades;
	int local;
	int local_rank;
	int fina_ranl;
};

vector <Node> list;

int local, N;

bool cmp(Node n1, Node n2) {
	if (n1.grades > n2.grades)
		return true;
	else if (n1.grades == n2.grades && n1.id < n2.id) {
		return true;
	}
	return false;
}

bool cmpLocal(Node n1, Node n2) {
	return n1.local < n2.local;
}

int main() {

	cin >> local;

	Node temp;
	
	vector <int> localnum ;

	int rank , tempGrande ;
	
	for (int i = 0; i < local; i++) {
		cin >> N;
		localnum.push_back(N);
		int st = list.size();
		for (int j = 0; j < N; j++) {
			cin >> temp.id >> temp.grades;
			temp.local = i+1;
			list.push_back(temp);
		}
		int ed = list.size();
		tempGrande = -1;
//		cout << st << " ed " << ed << endl;
		sort(&list[st], &list[st]+ N, cmp);
		for (int j = st, r = 1; j < ed; j++) {
			if (tempGrande != list[j].grades) {
				r = j - st + 1;
				tempGrande = list[j].grades;
			}
			list[j].local_rank = r;
		}

	}

#ifdef _DEBUG
	cout << endl;
	for (int i = 0; i < list.size(); i++) {
		cout << list[i].id << " " << list[i].grades << " " <<  list[i].fina_ranl << " "
			<< list[i].local << " " << list[i].local_rank <<  endl;
	}
	cout << endl;
#endif
	

	int len = list.size();
	//最终排序
	sort(&list[0], &list[0] + len, cmp);
	tempGrande = -1;
	for (int i = 0,r = 0; i < len; i++) {
		if (tempGrande != list[i].grades) {
			r = i+1; tempGrande = list[i].grades;
		}
		list[i].fina_ranl = r;
	}


	cout << list.size() << endl;
	for (int i = 0; i < list.size(); i++) {
		cout << list[i].id << " " << list[i].fina_ranl << " "
			<< list[i].local << " " << list[i].local_rank << endl;
	}

	system("pause");

	return 0;
	
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值