Programming Ability Test (PAT) (1025)第三个测试点(测试点id为2)出错-问题分析以及修改

题目描述如下

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.

提交的代码

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

typedef struct student{
    char id[15];
    int score=0;
    int group = 0;
    int org_rank=0;
} student_info;

bool cmp(student_info a, student_info b){
    if(a.score != b.score){
        return a.score > b.score;
    }
    // if else(a.group!=b.group){
    //     return a.group<b.group;
    // }
    else{
        return strcmp(a.id,b.id)<0;
    }
}

int main(){
    int N;
    scanf("%d",&N);
    student_info student_info_list[N*301];
    int group[N+1];
    int num=1;
    for(int i=1; i<=N; i++){
        // int k;
        scanf("%d",&group[i]);
        int j=1;
        for(j=1;j<= group[i];j++){
            scanf("%s %d",student_info_list[num].id,&student_info_list[num].score);
//             printf("%s\n",student_info_list[num].id);
            student_info_list[num].group=i;
            student_info_list[num].org_rank = j;
            num++;
        }
    }
    // cout<<"have done input"<<endl;
    cout<<num-1<<endl;
    sort(student_info_list+1,student_info_list+num,cmp);
    int last_score=0,last_score_id=1;
    int last_score_group[N+1],last_score_id_group[N+1],num_group[N+1];
    memset(last_score_group,0,sizeof(last_score_group));
    memset(last_score_id_group,1,sizeof(last_score_id_group));
    memset(num_group,0,sizeof(num_group));
    for(int i=1;i<num;i++){
        num_group[student_info_list[i].group]++;
        if(student_info_list[i].score != last_score){
            last_score_id = i; 
            last_score = student_info_list[i].score;
        }
        if(last_score_group[student_info_list[i].group]!=student_info_list[i].score){
            last_score_id_group[student_info_list[i].group]=num_group[student_info_list[i].group];
            last_score_group[student_info_list[i].group]=student_info_list[i].score;
        }
        printf("%s %d %d %d",student_info_list[i].id, last_score_id,student_info_list[i].group,last_score_id_group[student_info_list[i].group]);
        if(i!=num-1){
            printf("\n");
        }
    }
    
    return 0;
}
提交结果:卡在第三个测试点(id为2的测试点)。

在这里插入图片描述

问题分析

在我的代码逻辑里面,默认所考生分数大于零,这时在每个判断初始,都会将全局,考场排名初始化为1(预设为0)。
但是有可能出现,所有考生的成绩全为0,在这时,输出的考生排名全为0。
所以需要将,考生的初始排名设为1或者将第一名的考生的排名设为1.

代码修改

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

typedef struct student{
    char id[15];
    int score=0;
    int group = 0;
    int org_rank=0;
} student_info;

bool cmp(student_info a, student_info b){
    if(a.score != b.score){
        return a.score > b.score;
    }
    // if else(a.group!=b.group){
    //     return a.group<b.group;
    // }
    else{
        return strcmp(a.id,b.id)<0;
    }
}

int main(){
    int N;
    scanf("%d",&N);
    student_info student_info_list[N*301];
    int group[N+1];
    int num=1;
    for(int i=1; i<=N; i++){
        // int k;
        scanf("%d",&group[i]);
        int j=1;
        for(j=1;j<= group[i];j++){
            scanf("%s %d",student_info_list[num].id,&student_info_list[num].score);
//             printf("%s\n",student_info_list[num].id);
            student_info_list[num].group=i;
            student_info_list[num].org_rank = j;
            num++;
        }
    }
    // cout<<"have done input"<<endl;
    cout<<num-1<<endl;
    sort(student_info_list+1,student_info_list+num,cmp);
    int last_score=0,last_score_id=1; //初始化全局排名为1
    int last_score_group[N+1],last_score_id_group[N+1],num_group[N+1];
    memset(last_score_group,0,sizeof(last_score_group));
    memset(last_score_id_group,0,sizeof(last_score_id_group));
    memset(num_group,0,sizeof(num_group));
    for(int i=1;i<num;i++){
        num_group[student_info_list[i].group]++;
        if(student_info_list[i].score != last_score ){
            last_score_id = i; 
            last_score = student_info_list[i].score;
        }
        if(last_score_group[student_info_list[i].group]!=student_info_list[i].score || num_group[student_info_list[i].group]==1){ //将考场第1考生的排名初始化为1
            last_score_id_group[student_info_list[i].group]=num_group[student_info_list[i].group];
            last_score_group[student_info_list[i].group]=student_info_list[i].score;
        }
        printf("%s %d %d %d",student_info_list[i].id, last_score_id,student_info_list[i].group,last_score_id_group[student_info_list[i].group]);
        if(i!=num-1){
            printf("\n");
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值