1095 解码PAT准考证 段错误、运行时错误

写在前面
  • ac总结
    • 合适的数据结构
    • 少量循环
    • 最后行换行符问题
  • 问题记录
    • for循环过多,3 4 5 段错误、运行时异常
    • cin、cout 修改为 scanf(输入无换行符,字符串输入特殊处理)、printf(字符串输出特殊处理)
    • 结构体使用(传参、排序)
    • 代码框架
试题详情
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
示例代码
#include <iostream>
#include<algorithm>
#include<unordered_map>
#include<vector>
using namespace std;
struct node
{
    string s;
    int val;
} ;

bool cmp(node l, node r)
{
    if(l.val==r.val) return l.s.compare(r.s)<=0;
    else return l.val>r.val;
}

// 首字母相同,非递减排序输出
void sort_print_1(struct node *sp, string tar, int pn)
{
    int tar_cnt=0;
    struct node ptab[pn];
    for(int i=0; i<pn; i++)
        if(sp[i].s.substr(0,1) == tar.substr(0,1))
        {
            ptab[tar_cnt++] = sp[i];
        }
    sort(ptab,ptab+tar_cnt,cmp);
    if(tar_cnt<=0)
    {
        printf("NA");
    }
    else
    {
        for(int i=0; i<tar_cnt-1 ; i++)
        {
            printf("%s %d\n",ptab[i].s.c_str(),ptab[i].val);
        }
        printf("%s %d",ptab[tar_cnt-1].s.c_str(),ptab[tar_cnt-1].val); // 处理最后行数据
    }
}

// 统计指定考场人数,分数
void get_score_2(struct node *ptab, string tars, int pcnt)
{
    int num=0, tsocre=0;
    for(int i=0; i<pcnt; i++)
        if(ptab[i].s.substr(1,3) == tars)
        {
            num++;
            tsocre += ptab[i].val;
        }
    if(num<=0) printf("NA");
    else printf("%d %d",num, tsocre);
}

// 根据指定日期,分别计算考场人数,非递减排序输出
void get_kc_rs_3(struct node *ptab, string tars, int pcnt)
{
    vector<node> ans;
    unordered_map<string, int> kcbh_rs;
    for(int i=0; i<pcnt; i++) if(ptab[i].s.substr(4,6) == tars) kcbh_rs[ptab[i].s.substr(1,3)]++;
    for(auto itr: kcbh_rs)ans.push_back({itr.first, itr.second});
    sort(ans.begin(), ans.end(),cmp);
    if(ans.size()<=0)
    {
        printf("NA");
    }
    else
    {
        int ans_size = ans.size();
        for (int j = 0; j < ans_size-1; j++) printf("%s %d\n", ans[j].s.c_str(), ans[j].val);
        printf("%s %d", ans[ans_size-1].s.c_str(), ans[ans_size-1].val);
    }
}



int main()
{
    int n,m;
    scanf("%d %d", &n,&m);
    char zkzh[13];
    struct node tab[n];
    for(int i=0; i<n; i++)
    {
        scanf("%s %d",zkzh, &tab[i].val);
        tab[i].s = zkzh;
    }

    int zs;
    for(int i=0; i<m; i++)
    {
        scanf("%d %s",&zs, zkzh);
        printf("Case %d: %d %s\n",i+1,zs,zkzh);
        switch(zs)
        {
        case 1:
            sort_print_1(tab, zkzh, n);
            break;
        case 2:
            get_score_2(tab, zkzh, n);
            break;
        case 3:
        {
            get_kc_rs_3(tab, zkzh, n);
            break;
        }
        }
        if(i<m-1) printf("\n");
    }
    return 0;
}
心得体会
  • 磨磨唧唧,一个多月的时间,期间很长一段时间都没有认真刷题;最近一个周才开始恢复常态,经过多次尝试,才ac掉。
  • 一方面,多做笔记;一方面多思考,构建自己的解题思路。
  • 前几次尝试未使用第三方库,分拆了T、A、B3个数组,循环较多导致段错误、运行时错误,读者可以注意类似问题。
  • 上述题目相对基础,注重细节、代码量略大。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

xsimah

创作不易,感谢客官的打赏

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

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

打赏作者

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

抵扣说明:

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

余额充值