码迷,mamicode.com
首页 > 其他好文 > 详细

pat 1025

时间:2020-07-14 17:59:25      阅读:48      评论:0      收藏:0      [点我收藏+]

标签:file   string   line   ranking   cin   imu   nbsp   turn   ext   

1025 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 (≤), the number of test locations. Then N ranklists follow, each starts with a line containing a positive integer K (≤), 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

题意:给定n个考场的pat考生成绩,要求按所有考试的排名输出,并且要求输出考生所属考场以及所属考场排名。

思路:用一个vector保存所有考生成绩、id,对这个vector用sort函数排序,得到所有考生的分数排序。

   用n个vector保存每个考场的考生成绩,id,排序,得到每个考场所有考生的分数排序。

   题目要求同分数的排名应该相同,因此node结点里面设置两个变量:rank表示总排名,local_rank表示所属考场排名。对相同成绩的考生进行处理,使其(local_)rank相同,如何处理参考第二个和第三个for循环。

代码如下:

#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
#include<map>
using namespace std;
struct node{
    string id;
    int socre;
    int local_num;
    int rank;
    int local_rank;
};
bool cmp(node a,node b){
    if(a.socre!=b.socre)
        return a.socre>b.socre;
    else
        return a.id<b.id;
}
vector<node> stu;
vector<node> stu_local[105]; 
int main(){
    int n,k;
    scanf("%d",&n);
    int socre;
    string id;
    for(int i=0;i<n;i++){
        scanf("%d",&k);
        for(int j=0;j<k;j++){
            cin>>id>>socre;
            node temp;
            temp.id=id;
            temp.local_num=i+1;
            temp.socre=socre;
            stu.push_back(temp);
            stu_local[i].push_back(temp);
        }
        sort(stu_local[i].begin(),stu_local[i].end(),cmp);    
    }
    sort(stu.begin(),stu.end(),cmp);
    int rank=1;
    for(int i=0;i<stu.size();i++){
        int j=i;
        stu[j].rank=rank;
        while(i+1<stu.size()&&stu[i+1].socre==stu[j].socre){
            
            stu[i+1].rank=rank;
            i++;
        }
        rank=rank+i-j+1;
    }
    map<string,int> m;
    for(int i=0;i<n;i++){
        int local_rank=1;
        
        for(int j=0;j<stu_local[i].size();j++){
            int k=j;
            stu_local[i][k].local_rank=local_rank;
            m[stu_local[i][k].id]=local_rank;
            while(j+1<stu_local[i].size()&&stu_local[i][j+1].socre==stu_local[i][k].socre){
                
                stu_local[i][j+1].local_rank=local_rank;
                m[stu_local[i][j+1].id]=local_rank;
                j++;
            }
            local_rank=local_rank+j-k+1;
        }
    }
    printf("%d\n",stu.size());
    for(int i=0;i<stu.size();i++){
        printf("%s %d %d %d\n",stu[i].id.c_str(),stu[i].rank,stu[i].local_num,m[stu[i].id]);
    }
    return 0;
} 

 

pat 1025

标签:file   string   line   ranking   cin   imu   nbsp   turn   ext   

原文地址:https://www.cnblogs.com/foodie-nils/p/13300198.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!