1025. PAT Ranking (25)

1.题目给出各个区域的用户分数,求出最终的区域排名和总排名。

2.用string记录ID。

3.熟悉sort函数的用法,选取合适的范围排序。

4.题目要求:相同分数的排名相同,分数相同的情况下ID需要按照递增排列。

5.处理排名,相同分数的学生,排名相同。如100,90,90,80,排名为1,2,2,4。

时间限制
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
//#include<string>
//#include <iomanip>
#include<vector>
#include <algorithm>
//#include<stack>
#include<set>
#include<queue>
#include<map>
//#include<unordered_set>
//#include<unordered_map>
//#include <sstream>
//#include "func.h"
//#include <list>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
using namespace std;
/*
2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
4
1234567890013 65
1234567890011 25
1234567890014 100
1234567890012 85

2
5
1234567890001 95
1234567890005 100
1234567890003 95
1234567890002 77
1234567890004 85
1
1234567890013 65

2
5
1234567890005 100
1234567890003 95
0000567890001 95
1234567890002 77
1234567890004 85
0

*/
struct studentNode{
	string id;
	int location, finalRank, localRank,score;
	studentNode() :id(""), location(-1), finalRank(-1), localRank(-1),score(-1){};
	studentNode(string i, int s) :id(i), location(-1), finalRank(-1), localRank(-1), score(s){};
};
bool cmp(const studentNode& a, const studentNode&b)
{
	if (a.score > b.score)
		return true;
	else if (a.score == b.score&&a.id < b.id)//分数相等的,ID按照递增排列
		return true;
	else return false;
}
int main(void)
{
	vector<studentNode> student(0);
	int locationNum, studentNum = 0;
	cin >> locationNum;//输入排名表的个数
	for (int location = 0; location < locationNum; location++)
	{
		int testNum; cin >> testNum;//输入每个排名表的学生数量
		if (testNum > 0)
		{
			for (int i = 0; i < testNum; i++)
			{
				string id; int score;
				cin >> id >> score;
				student.push_back(studentNode(id, score));
				student.back().location = location + 1;
				studentNum++;
			}

			//对排名表区域内的学生进行排序
			sort(student.begin() + studentNum - testNum, student.begin() + studentNum, cmp);
			student[studentNum - testNum].localRank = 1;
			for (int i = studentNum - testNum + 1; i < studentNum; i++)
			{//处理排名,相同分数的学生,排名相同。如100,90,90,80,排名为1,2,2,4
				if (student[i].score == student[i - 1].score)
					student[i].localRank = student[i - 1].localRank;
				else
					student[i].localRank = i - (studentNum - testNum ) + 1;
			}
		}
	}
	if (studentNum > 0)
	{//如果学生数量大于0,进行排序和统计排名
		sort(student.begin(), student.end(), cmp);
		student[0].finalRank = 1;
		for (int i = 1; i < studentNum; i++)
		{
			if (student[i].score == student[i - 1].score)
				student[i].finalRank = student[i - 1].finalRank;
			else
				student[i].finalRank = i + 1;
		}
	}
	cout << studentNum << endl;
	for (int i = 0; i < studentNum; i++)
	{
		cout << student[i].id << " " << student[i].finalRank << " " << student[i].location << " " << student[i].localRank << endl;
	}
	return 0;
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注