Map和Vector

发布时间 2023-04-18 20:04:07作者: muqi_lu

PAT甲级-1016

sanple input

10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10
10
CYLL 01:01:06:01 on-line
CYLL 01:28:16:05 off-line
CYJJ 01:01:07:00 off-line
CYLL 01:01:08:03 off-line
CYJJ 01:01:05:59 on-line
aaa 01:01:01:03 on-line
aaa 01:02:00:01 on-line
CYLL 01:28:15:41 on-line
aaa 01:05:02:24 on-line
aaa 01:04:23:59 off-line

sample output

CYJJ 01
01:05:59 01:07:00 61 $12.10
Total amount: $12.10
CYLL 01
01:06:01 01:08:03 122 $24.40
28:15:41 28:16:05 24 $3.85
Total amount: $28.25
aaa 01
02:00:01 04:23:59 4318 $638.80
Total amount: $638.80

通过代码

#include<iostream>
#include<map>
#include<algorithm>
#include<vector>
using namespace std;

struct record{
    int dd,hh,mm,t;
    string tag;
};

int N;
double danjia[24];
map<string,vector<record>>M;

bool comp(record x,record y){
    return x.t<y.t;
}

int main(){
    int i,j,k;
    int month;
    for(i=0;i<24;i++){
        cin>>danjia[i];
    }
    cin>>N;
    for(i=0;i<N;i++){
        string name,tag;
        char c;
        int date,hour,minute;
        cin>>name>>month>>c>>date>>c>>hour>>c>>minute>>tag;
        record temp;
        temp.dd=date;
        temp.hh=hour;
        temp.mm=minute;
        temp.t=date*1440+hour*60+minute;
        temp.tag=tag;
        M[name].emplace_back(temp);
    }
    for(auto it=M.begin();it!=M.end();++it){
        auto V=it->second;
        sort(V.begin(),V.end(),comp);
        double total=0;
        for(i=0;i<V.size();){
            if(i+1<V.size()&&V[i].tag>V[i+1].tag){
                if(!total){
                    cout<<it->first;
                    printf(" %02d\n",month);
                }
                int t1=V[i].t;
                int t2=V[i+1].t;
                double fenzhang=0;
                for(int Time=t1;Time<t2;++Time){
                    fenzhang+=danjia[Time%1440/60];
                }
                printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",V[i].dd,V[i].hh,V[i].mm,V[i+1].dd,V[i+1].hh,V[i+1].mm,V[i+1].t-V[i].t,fenzhang/100);
                i+=2;
                total+=fenzhang;
            }
            else{
                i++;
            }
        }
        if(total)printf("Total amount: $%.2f\n",total/100);
    }
    return 0;
}