程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 1025. PAT Ranking (25)

1025. PAT Ranking (25)

編輯:C++入門知識

題目描述: 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 分析: (1)對結構體排序並且合並。 (2)注意grade一樣的情況下的處理。     參考代碼:

#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;
}

 


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved