PAT A1025

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, 对于每个测试用例,第一行是N正数,the first line contains a positive number N (≤100), the number of test locations(表示测试点的数量). Then N ranklists follow, 接下来给出N个排行榜单 each starts with a line containing a positive integer K (≤300),每行开始有一个数K  the number of testees, 测试对象的数量 and then K lines containing the registration number (a 13-digit number) 接下来K行是注册号码,一个13位的数字 ,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. N的取值范围位1-N,

The output must be sorted in nondecreasing order of the final ranks.输出应该按照final排行的非下降顺序。 

The testees with the same score must have the same rank, 分数相同的人排名页相同 t

he 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

by a space.

常用词: a positive numbe 一个正数 testees 测验对象 specification规格 sorted 被妥善处理的 be sorted in 按照

生词:simultaneously 同时地  generate 生成,生产

思路:考察将几个数组合并成一个数组并进行排序 。考虑sort排序,结构体,cmp函数

常见的输入输出的坑 在读入%s之前用getchar清理一下换行或者空格。

这个是过了样例的代码,只过了样例,发生了其他错误:数组越界、答案错误。

#include <cstdio>
#include <algorithm>
using namespace std;
const int maxSize=30010;
struct Testee {
    int score;
    char registration_number[14];
    int location_number;
    int local_rank;
    int final_rank;
}final[maxSize];
//按照分数从大到小排列,与按排名从小到大排列一样,分数相同,按照号码从小到大排列
bool cmp(Testee a,Testee b)
{
    return a.score==b.score?a.registration_number<b.registration_number:a.score>b.score;
}
int main()
{
    int N,K,current=0,i,j,k,p,q,location_num=1;
    scanf("%d",&N);
    for (int q = 0; q < N; ++q) {
        scanf("%d",&K);
        for (i = 0; i < K;i++) {
            getchar();
            scanf("%s %d",&final[current+i].registration_number,&final[current+i].score);
            final[current+i].location_number = location_num;final[current+i].final_rank=1;final[current+i].local_rank=1;
        }
        sort(final+current,final+current+K,cmp);
        for (j = 1; j < K; j++) {
            if(final[j+current-1].score==final[j+current].score)
                final[j+current].local_rank = final[current+j-1].local_rank;
            else
                final[j+current].local_rank = j +1;
        }
        current +=K;
        location_num++;
    }
    sort(final,final+current,cmp);
    for (k = 1; k < current; k++) {
        if(final[k-1].score==final[k].score)
            final[k].final_rank = final[k-1].final_rank;
        else
            final[k].final_rank = k+1;
    }
    printf("%d\n",current);
    for (p = 0; p < current; p++)
    {
        printf("%s %d %d %d\n",final[p].registration_number,final[p].final_rank,final[p].location_number,final[p].local_rank);
    }
    return 0;
}
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
struct Testee {
    int score;
    char id[15];
    int location;
    int local_rank;
}final[30010];
//按照分数从大到小排列,与按排名从小到大排列一样,分数相同,按照号码从小到大排列
bool cmp(Testee a,Testee b)
{
    if(a.score!=b.score) return a.score>b.score;
    else  return strcmp(a.id, b.id) < 0;
}
int main()
{
    int N,K,num=0;
    scanf("%d",&N);
    for (int q = 1; q <= N; ++q) {//N考场
        scanf("%d",&K);
        int begin = num;
        for (int i = 0; i < K;i++) {//每组K个账号信息
            scanf("%s %d",&final[num].id,&final[num].score);
            final[num].location = q;
            num++;
        }
        sort(final+begin,final+num,cmp);//本地排序
        int local_rank = 1;
        final[begin].local_rank = 1;
        for (int j = 0; j < K; j++) {//本地排名信息
            if(j>0&&final[j-1+begin].score!=final[j+num-K].score)
            {
                local_rank = j+1;
            }
            final[j+begin].local_rank = local_rank;
        }
    }
    printf("%d\n",num);
    sort(final,final+num,cmp);//总排名
    int final_rank = 1;
    for (int p = 0; p < num; p++)
    {
        if(p>0&&final[p].score!=final[p-1].score)
        {
            final_rank = p+1;
        }
        printf("%s %d %d %d\n",final[p].id,final_rank,final[p].location,final[p].local_rank);
    }
    return 0;
}

总结:cmp写错了,粗心

收获:粗心-1,细心+1

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值