PAT (Basic Level) Practice (中文)1095 解码PAT准考证(c语言)


思路 

1、对考生的准考证,因为是固定格式,使用sscanf函数来处理很方便。

2、对类型1:先按要求排序,再遍历输出即可,比较简单。

3、对类型2:同样是遍历输出即可,比较简单,都要注意没有结果时的输出即可,用flag来控制。

4、对类型3:这个相对难一点,建立一个type3结构体来进行统计处理,记录考场编号和人数,最后根据要求排序输出即可。

5、对类型3,需要进行日期的输出,注意要%06d,不然测试点1、4过不去。


AC代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct{//考生结构体
    char str[14],level;
    int examno,examdata,id,score;
}student;
typedef struct{//类型3统计使用
    int examno,cnt;
}type3;
int cmp1(const void* a,const void* b){//按类型1的要求排序
    student* pa=(student*)a;
    student* pb=(student*)b;
    if(pb->score == pa->score)return strcmp(pa->str,pb->str);
    return pb->score - pa->score;
}
int cmp3(const void* a,const void* b){//按类型3的要求排序
    type3* pa=(type3*)a;
    type3* pb=(type3*)b;
    if(pa->cnt == pb->cnt)return pa->examno - pb->examno;
    return pb->cnt - pa->cnt;
}
int main(){
    int n,m;
    char temp[14];
    scanf("%d %d%*c",&n,&m);
    student st[n];
    for(int i=0;i<n;i++){//接受学生数据并处理
        scanf("%s %d%*c",st[i].str,&st[i].score);
        sscanf(st[i].str,"%c%3d%6d%3d",&st[i].level,&st[i].examno,&st[i].examdata,&st[i].id);
    }
    qsort(st,n,sizeof(student),cmp1);
    for(int i=1;i<=m;i++){
        int flag=0,k=0,type,num;
        char ch;
        scanf("%d",&type);
        if(type==1){//类型1
            scanf("%*c%c",&ch);
            printf("Case %d: %d %c\n",i,type,ch);
            for(int j=0;j<n;j++){
                if(st[j].level==ch){
                    printf("%s %d\n",st[j].str,st[j].score);
                    flag=1;
                }
            }
            if(flag==0)printf("NA\n");//注意没有查询结果的输出
        }
        else if(type==2){//类型2
            scanf("%d",&num);
            printf("Case %d: %d %d\n",i,type,num);
            int cnt=0,sum=0;
            for(int j=0;j<n;j++){
                if(st[j].examno==num){
                    flag=1;
                    cnt++;
                    sum+=st[j].score;
                }
            }
            if(flag!=0)printf("%d %d\n",cnt,sum);
            else printf("NA\n");//注意没有查询结果的输出
        }
        else{//类型3
            scanf("%d",&num);
            printf("Case %d: %d %06d\n",i,type,num);//注意输出日期时要%06d,不然测试点1和4过不了
            type3 ty[1000];
            for(int j=0;j<1000;j++)ty[j].cnt=0;//要初始化结构体的cnt,即每个考场指代的人数
            for(int j=0;j<n;j++){//遍历学生,选择对应日期的进行统计
                if(st[j].examdata==num){
                    ty[st[j].examno].examno=st[j].examno;//先用下标指代考场编号,之后再排序
                    ty[st[j].examno].cnt++;
                }
            }
            qsort(ty,1000,sizeof(type3),cmp3);
            for(int j=0;j<1000;j++){
                if(ty[j].cnt!=0){//人数不为零的输出
                    printf("%d %d\n",ty[j].examno,ty[j].cnt);
                    flag=1;
                }
                else break;//遇到人数为零的就终止
            }
            if(flag==0)printf("NA\n");//注意没有查询结果的输出
        }
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Dragon_dd

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

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

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

打赏作者

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

抵扣说明:

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

余额充值