前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >1095 解码PAT准考证 (25 分)

1095 解码PAT准考证 (25 分)

作者头像
可爱见见
发布2019-11-20 15:32:22
4590
发布2019-11-20 15:32:22
举报
文章被收录于专栏:卡尼慕卡尼慕

1095 解码PAT准考证 (25 分)

【我的代码】

//1095 解码PAT准考证 (25 分)
/*
第 1 位是级别,即 T 代表顶级;A 代表甲级;B 代表乙级;
第 2~4 位是考场编号,范围从 101 到 999;
第 5~10 位是考试日期,格式为年、月、日顺次各占 2 位;
最后 11~13 位是考生编号,范围从 000 到 999。
*/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;
struct student{
    string id;
    int score;
};
struct examcase2{
    int allscore=0;
    int people=0;
};
struct examroom{
    string room;
    int people=0;
};
bool cmp(student a,student b)
{
    if(a.score!=b.score) return a.score>b.score;
    else return a.id<b.id;
}
bool cmp1(examroom a,examroom b)
{
    if(a.people!=b.people) return a.people>b.people;
    else return a.room<b.room;
}
int main()
{
    int N,M;
    scanf("%d %d",&N,&M);  
    map<string,vector<student>>  case1;     
    map<string,examcase2> case2;            
    map<string,map<string,examroom>>  case3;   
    for(int i = 0; i < N; i++)
    {
        student s;
        char id[14];
        scanf("%s %d",id,&s.score);
        s.id=id;
        string ranks=s.id.substr(0,1);
        string exroom=s.id.substr(1,3);
        string date=s.id.substr(4,6);
        case1[ranks].push_back(s);        
        case2[exroom].allscore+=s.score;    
        case2[exroom].people++;           
        case3[date][exroom].room=exroom;   
        case3[date][exroom].people++;
    }
    for(int i=1;i<=M;i++)
    {
        int classi;
        char request[7];
        memset(request,0,sizeof(request));
        scanf("%d %s",&classi,request);
        printf("Case %d: %d %s\n",i,classi,request);
        int flag=1;
        switch(classi)
        {
            case 1:
                {
                    vector<student>  ttt=case1[request];    
                    sort(ttt.begin(),ttt.end(),cmp);        
                    for(auto it=ttt.begin();it!=ttt.end();it++)
                    {
                        printf("%s %d\n",(*it).id.c_str(),(*it).score);
                        flag=0;                   
                    }
                    break;
                }
            case 2:
                {
                    examcase2 ttt=case2[request];       
                    if(ttt.people!=0)               
                    {
                        printf("%d %d\n",ttt.people,ttt.allscore);  
                        flag=0;
                    }
                    break;
                }
            case 3:
                {
                    map<string,examroom> ttt=case3[request]; 
                    int i=0,len=ttt.size();       
                    examroom e[len];
                    for(auto it=ttt.begin();it!=ttt.end();it++)
                    {
                        e[i++]=it->second;        
                    }
                    sort(e,e+len,cmp1);   
                    for(i=0;i<len;i++)
                    {
                        printf("%s %d\n",e[i].room.c_str(),e[i].people);
                        flag=0;
                    }
                    break;
                }
        }
        if(flag)  printf("NA\n");    
    }
    return 0;
}

【总结】

果然不愧是最后一题,复杂度还是比较大的。题目也很长,不过仔细看看题目就会发现这也就是个纸老虎,逻辑上没有很大的难度,难就难在,他把三个小问题结合成一个大问题来考查了。

  • 类型 为 1 表示要求按分数非升序输出某个指定级别的考生的成绩,对应的 指令 则给出代表指定级别的字母;
  • 类型 为 2 表示要求将某指定考场的考生人数和总分统计输出,对应的 指令 则给出指定考场的编号;
  • 类型 为 3 表示要求将某指定日期的考生人数分考场统计输出,对应的 指令 则给出指定日期,格式与准考证上日期相同。

于是,针对三个小题目,我们就“单独分开”来做,针对题目建立三个相关的结构体和处理映射关系的map。

学生(用于问题一排序输出):

struct student{
    string id;
    int score;
};

考场一(用于解决问题二,统计总分的情况):

struct examcase2{
    int allscore=0;
    int people=0;
};

考场二(用于解决问题三需要分考场统计输出):

struct examroom{
    string room;
    int people=0;
};

map:

    map<string,vector<student>>  case1;     //按等级分;
    map<string,examcase2> case2;             //按教室分;
    map<string,map<string,examroom>>  case3;     //按日期分;

处理输入问题。根据题目给的提示与要求,我就老老实实按部就班就完事了。分别对应了等级,考场号,考试日期。

        string ranks=s.id.substr(0,1);
        string exroom=s.id.substr(1,3);
        string date=s.id.substr(4,6);

最后我们处理好输入以后,就是针对问题进行输出。

这里需要的注意一点就是遍历map的时候。

for(auto it=ttt.begin();it!=ttt.end();it++){
        printf("%s %d\n",(*it).id.c_str(),(*it).score);
        flag=0; 
}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-11-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 卡尼慕 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档