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

(PAT甲级)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 (≤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 <stdio.h>
#include <algorithm>
using namespace std;
struct testee{
        long long registration_number;
        int finl_rank;
        int location_number;
        int local_rank;
        int score;
    }a[30010];
bool cmp(testee a,testee b){
     if(a.score!=b.score) return a.score>b.score;
     else return a.registration_number<b.registration_number;
}
int main()
{
    
   
    int n,i=0;
    scanf("%d",&n);
    int k;
    for(int m=0;m<n;m++){
        scanf("%d",&k);
        for(int h=0;h<k;h++){
            a[i].location_number=m+1;
            scanf("%ld%d",&a[i].registration_number,&a[i].score);
            i++;
        }
        sort(a+i-k,a+i,cmp);
        a[i-k].local_rank=1;
      
        for(int h=i-k+1;h<i;h++){
            if(a[h].score==a[h-1].score) a[h].local_rank=a[h-1].local_rank;
            else a[h].local_rank=h-(i-k)+1;
       

        }
    }
    sort(a,a+i,cmp);
    a[0].finl_rank=1;
 
    for(int h=1;h<i;h++){
        if(a[h].score==a[h-1].score) a[h].finl_rank=a[h-1].finl_rank;
        else a[h].finl_rank=h+1;
        
    }
    printf("%d\n",i);
    for(int h=0;h<i;h++){
        printf("%013ld %d %d %d\n",a[h].registration_number,a[h].finl_rank,a[h].location_number,a[h].local_rank);
    }
    return 0;

}

1.思路:根据题意可定义一个结构体,之后用sort排序,排序分为两次,一次是同一测试地点的排序,二是全体考生的排序
2.错误点
(1)对同一测试点的考生进行排序的话可以直接在读取完这个地点的所有考生信息的for循环后,在整体的for循环内,i为当前所读取的考生的总数

(2)关于排序
bool cmp(testee a,testee b){
if(a.score!=b.score) return a.score>b.score;//当分数不等时,按分数排序
else return a.registration_number<b.registration_number;
//当分数相等时,按考生登记号排序
}

(3)排序后对学生排名次时,当分数相同时名次相同,所以如果上一名同学与上上一名同学分数相同,名次相同,下面同学的名次不能在等于上一个同学的名次,所以当分数不同时,可以让他等于他是第几个数,这样就是第几名
(4)关于学生登记号的存取,是使用数组还是使用long long型,(int不行,超出了他的范围,10的9次方以内都可以用int,10的16次方以内可以用long long),但是使用long long存储的话这个题有一个坑,在第四个测试点的时候会出错,原因是使用long long型存储如果id是以0开头的会发生错误,如0002存储后会成2,所以输出的时候要加上**%013d**,所以如果是存储id的话建议使用数组

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值