PTA乙题1095 解码PAT准考证 (25分)

博客介绍了PAT乙题1095的解题思路,强调了在处理大量字符串输入时避免使用cin和cout,推荐使用字符数组。博主分享了在debug过程中遇到的困难,并提到scanf函数在分割和处理字符串时的高效性。
摘要由CSDN通过智能技术生成

在这里插入图片描述

输入样例

8 4
B123180908127 99
B102180908003 86
A112180318002 98
T107150310127 62
A107180908108 100
T123180908010 78
B112160918035 88
A107180908021 98
1 A
2 107
3 180908
2 999

输出样例

Case 1: 1 A
A107180908108 100
A107180908021 98
A112180318002 98
Case 2: 2 107
3 260
Case 3: 3 180908
107 2
123 2
102 1
Case 4: 2 999
NA

先看看AC代码,速度非常的快,基本胜过CSDN所有的朋友。(装个小x)

然后我总结一下这道题遇到的一些阻碍。
其实这个题本身想法上是很顺畅的,没有什么难度。但是debug的时候有些错误很难找到。如果考试中出现这道题,需要好的心态,因为我编了一个小时才ac。

  • 首先这种10000数量级的字符串读入操作,不要使用c++的cin和cout,这样会非常耗时。同时出现一个问题就是也不能用string容器了,因为string只能用cin读取(当然如果非要用的话需要申请空间,所以干脆别用)。因此一切字符串都会用**字符数组存储。**那么一些取子串、复制的操作就没有趁手的函数了,这一有一个神来之笔的函数就是sccanf().
    见源代码
    看到了吗,把原来的整个字符串分割成好几部分赋值给其他的变量。同时也可以实现复制操作,和取子串操作。
  • 然后
    在这里插入图片描述
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cstring>

using namespace std;

struct student{
    char cardID[20];
    char level[10];
    char room[10];
    char date[10];
    char id[10];
    int score;
}S[10010];

struct type3{
    int room;
    int num=0;
};


bool cmp1(struct student a,struct student b){
    if(a.score!=b.score ) return a.score>b.score;
    else return strcmp(a.cardID,b.cardID)<0;
}

bool cmp3(struct type3 a,struct type3 b){
    if(a.num!=b.num ) return a.num>b.num;
    else return a.room<b.room;
}


int main(){
    int n,m;
    cin>>n>>m;
    for(int i=0;i<n;i++){
        scanf("%s %d",&S[i].cardID,&S[i].score);
        sscanf(S[i].cardID,"%1s%3s%6s%3s",&S[i].level,&S[i].room,&S[i].date,&S[i].id);
    }

    for(int i=1;i<=m;i++){
        int type=0;
        char ind[10];
        cin>>type>>ind;
        printf("Case %d: %d %s\n",i,type,ind);
        int cnt=0; \\这个很好用,每次循环都重置,如果没查到直接返回NA跳出去继续。
        struct student T[10010];
        
        if(type==1){
            for(int j=0;j<n;j++){
                if(strcmp(S[j].level,ind)==0) T[cnt++]=S[j];
            }
            if(cnt==0){
                printf("NA\n");
                continue;
            }
            sort(T,T+cnt,cmp1);
            
            for(int j=0;j<cnt;j++){
                printf("%s %d\n",T[j].cardID,T[j].score);
            }
            
        }else if(type==2){
            int total=0;
            for(int j=0;j<n;j++){
                if(strcmp(S[j].room,ind)==0){
                    cnt++;
                    total+=S[j].score;
                }
            }
            if(cnt==0){
                printf("NA\n");
                continue;
            }
            printf("%d %d\n",cnt,total);
        }else if(type==3){
            struct type3 Ty[1010];
  
            for(int j=0;j<n;j++){
                if(strcmp(S[j].date,ind)==0){
                    int roo = (S[j].room[0]-'0')*100+(S[j].room[1]-'0')*10+(S[j].room[2]-'0');\\也可以使用atoi()把字符串转化为整形数字,时间稍微多一点点。
                    Ty[roo].room=roo;
                    Ty[roo].num++;
                    cnt++;
                }
            }
           if(cnt==0){
                printf("NA\n");
               continue;
            }
            sort(Ty,Ty+1010,cmp3);\\这里一定要搞明白因为你开的数组里面有很多空的,所以一定不能只对人数那么多的结构体排序一定是全部的。
            for(int j=0;j<1010;j++){\\这里一样
                if(Ty[j].num!=0) printf("%d %d\n",Ty[j].room,Ty[j].num);
            }
        }
    }
        return 0;
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李烟云

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值