1016 Phone Bills (25)-PAT甲级真题

有坑,如果total费用为0的话,不能输出,包括用户名字和月份和总价格都不能输出

AC代码:

#include<bits/stdc++.h>
using namespace std;
int toll[25]={0},n;
struct node{
    int MM,dd,HH,mm,flag,time;
};
bool cmp(node a, node b){
    return a.time<b.time;
}
double cal_cent(node n){ //计算从头开始的费用,用于后面相减方便计算tot
    double tot=n.dd*60*toll[24]+n.mm*toll[n.HH];
    for(int i=0;i<n.HH;i++){
        tot+=toll[i]*60;
    }
    return tot/100;
}
map<string, vector<node>> m;
int main(){
    for(int i=0;i<24;i++){
        scanf("%d",&toll[i]);
        toll[24]+=toll[i];
    }
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        string name,temp;
        node nod;
        cin>>name; scanf("%d:%d:%d:%d",&nod.MM,&nod.dd,&nod.HH,&nod.mm);
        cin>>temp; nod.flag=temp[1]=='n'?1:0;
        nod.time=24*60*nod.dd+60*nod.HH+nod.mm;
        m[name].push_back(nod);
    }
    for(auto it:m){
        sort(it.second.begin(),it.second.end(),cmp);
        vector<node> temp=it.second;
        double tot=0.0,cost;
        bool first=1;// 作用:如果total费用为0的话,不能输出,包括用户名字和月份都不能输出
        for(int i=1;i<temp.size();i++){
            if(temp[i-1].flag==1&&temp[i].flag==0){
                cost=cal_cent(temp[i])-cal_cent(temp[i-1]);// 这一步的套路需要记住
                tot+=cost;
                if(first){
                    printf("%s %02d\n",it.first.c_str(),temp[0].MM);
                    first=0;
                }
                printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",temp[i-1].dd,temp[i-1].HH,
                       temp[i-1].mm,temp[i].dd,temp[i].HH,temp[i].mm,temp[i].time-temp[i-1].time,cost);
            }
        }
        if(!first) printf("Total amount: $%.2f\n",tot);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值