PAT甲级 —— 1025 PAT Ranking (25分)

  • 题目链接:PAT Ranking (25分)

  • 题目说明

    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<iostream>
    #include<string>
    #include<vector>
    #include<algorithm>
    using namespace std;
    
    typedef struct STU
    {
          
          
    	string num;
    	int score;
    	int loc;	
    }STU;
    
    vector<STU> student;
    
    bool cmp(STU a,STU b)
    {
          
          
    	if(a.score!=b.score)
    		return a.score>b.score;	//先按成绩降序
    	return a.num<b.num;			//再按学号升序
    }
    
    int main()
    {
          
          
    	STU s;
    	int locNum,stuNum;
    	
    	cin>>locNum;
    	for(int i=1;i<=locNum;i++)
    	{
          
          
    		cin>>stuNum;
    		for(int j=0;j<stuNum;j++)
    		{
          
          
    			s.loc = i;
    			cin>>s.num>>s.score;
    			student.push_back(s);
    		}
    	}
    	
    	cout<<student.size()<<endl;					//输出学生总人数  
    	sort(student.begin(),student.end(),cmp);	//排序 
    	
    	int globalRank = 1;			//当前总排名 
    	int locStu[110] = {
          
          0};		//每个考点已统计的学生人数 
    	int locScore[110] = {
          
          0};	//每个考点当前最低成绩 
    	int locRank[110];			//每个考点当前排名 
    	fill(locRank,locRank+110,1);
    	int loc;
    	for(int i = 0;i<student.size();i++)
    	{
          
          
    		loc = student[i].loc;
    		
    		if(i>=1 && student[i].score!=student[i-1].score)
    			globalRank=i+1;
    		
    		if(locStu[loc]>=1 && student[i].score!=locScore[loc])
    			locRank[loc]=locStu[loc]+1;
    		
    		locStu[loc]++;
    		locScore[loc] = student[i].score;
    		
    		cout<<student[i].num<<" "<<globalRank<<" "<<student[i].loc<<" "<<locRank[loc]<<endl; 
    	}
    		
    	return 0;
    }
    
  • 说明:

    • 这个题和乙级的德才论差不多,核心就是个排序,不过输出的时候要注意一下排名的处理:如果成绩和上一位不同,排名更新为已统计人数+1(下标从0开始)
    • 另外别忘了先输出总人数

猜你喜欢

转载自blog.csdn.net/wxc971231/article/details/108432190
今日推荐