[PAT][Basic level]1095

本文介绍了一个C++程序,通过字符串操作解析学生信息,包括姓名、分数、考试级别、考场号和日期,利用比较函数对数据进行排序。主要涉及字符串处理、整数转换和自定义排序。展示了如何使用`type_1`、`type_2`和`type_3`函数处理不同类型的操作。
摘要由CSDN通过智能技术生成

Keywords:string ;string.substr(pos_start,lenth);std::stoi(string,NULL,10);sort();

Not AC CODE:(3/5)

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std;
struct student{
    char num[14];
    int score;
    char level;
    int room;
    int date;
    int stuID;
};
struct classroom{
    int room_number;
    int stu_cnt = 0;
}classroom[1000];
void type_1(string &command,struct student * &stu,int &N);
void type_2(string &command,struct student * &stu,int &N);
void type_3(string &command,struct student * &stu,int &N);
bool cmp_stu(struct student a , struct student b){
    if(a.score == b.score){
        string str_a,str_b;
        str_a = a.num;
        str_b = b.num;
        return str_a < str_b;
    }
    else{
        return a.score >b.score;
    }
}
bool cmp_room(struct classroom a,struct classroom b){
    if(a.stu_cnt == b.stu_cnt){
        return a.room_number <b.room_number;
    }
    else{
        return a.stu_cnt >b.stu_cnt;
    }
}
int main()

{
    freopen("sample_input.txt","r",stdin);

    int N,M;
    cin>>N>>M;

    struct student *stu;
    stu = (struct student *)malloc(sizeof(struct student)*N);
    for(int i = 0 ; i < N ; i ++){
        cin>>stu[i].num>>stu[i].score;

        string str = stu[i].num;// convert to string
        string sub_str ;

        stu[i].level = str[0];//考试级别 T A B

        sub_str = str.substr(1,3);
        stu[i].room = std::stoi(sub_str,NULL,10);//考场

        sub_str = str.substr(4,6);
        stu[i].date = std::stoi(sub_str,NULL,10);//考试日期

        sub_str = str.substr(10,3);
        stu[i].stuID = std::stoi(sub_str,NULL,10);//考生编号

    }
    sort(stu,stu+N,cmp_stu);

    for(int i = 0 ; i < M ; i ++){
       int type;
       string command;
       cin>>type>>command;
       printf("Case %d: ",i+1);
       cout<<type<<" "<<command<<endl;
       if(type == 1){
        type_1(command,stu,N);
       }
       else if(type == 2){
        type_2(command,stu,N);
       }
       else if(type == 3){
        type_3(command,stu,N);
       }

    }

    return 0;
}
void type_1(string &command,struct student * &stu,int &N){
    char cmd = command[0];
    bool flag_found = false;
    for(int i= 0 ; i < N ; i ++){
        if(stu[i].level == cmd){
            cout<<stu[i].num<<" "<<stu[i].score<<endl;
            flag_found = true;
        }
    }
    if(!flag_found) cout<<"NA"<<endl;
}
void type_2(string &command,struct student * &stu,int &N){
    int room_cmd = std::stoi(command,NULL,10);
    int sum = 0;
    int cnt = 0;
    bool flag_found = false;
    for(int i = 0 ; i < N ; i++){
        if(stu[i].room == room_cmd){
            flag_found =true;
            sum += stu[i].score;
            cnt++;
        }
    }
    if(flag_found)
    cout<<cnt<<" "<<sum<<endl;
    else
    cout<<"NA"<<endl;
}

void type_3(string &command,struct student * &stu,int &N){
    int date_cmd = std::stoi(command,NULL,10);
    int j = 0;
    bool flag_found = false;
    for(int i = 0 ; i < N ; i++){
        if(date_cmd == stu[i].date){
            flag_found = true;
            classroom[stu[i].room].room_number = stu[i].room;
            classroom[stu[i].room].stu_cnt ++;

        }
    }

    if(flag_found){
        sort(classroom,classroom+1000,cmp_room);
        for(int i = 0 ; i < 1000 ; i ++){
                if(classroom[i].stu_cnt == 0) continue;
        cout<<classroom[i].room_number<<" "<<classroom[i].stu_cnt<<endl;
        }
    }
    else{
        cout<<"NA"<<endl;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值