其他分享
首页 > 其他分享> > [PAT][Basic level]1095

[PAT][Basic level]1095

作者:互联网

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;
    }

}

标签:1095,PAT,struct,level,int,stu,str,string,room
来源: https://blog.csdn.net/qq_38103468/article/details/118057696