pat 1016 Phone Bills

地址:https://pintia.cn/problem-sets/994805342720868352/problems/994805493648703488

模拟是真的烦,不仅要写一堆代码,还得读懂题,我做模拟真是越做越烦
坑点:
1>For each test case, you must print a phone bill for each customer.
这句话有歧义,我一直都输出花费为0的顾客,但是居然不输出
2>on-line与off-line按时间顺序紧挨着算配对成功

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
#define mp make_pair
#define pb push_back
#define fi first
#define se second

int bill[30]; //存时间段花费
//结构体排序
struct record{
    string ss;
    int m,d,h,s;
    int state;
    record(){}
    record(string ss,int m,int d,int h,int s,int state):ss(ss),m(m),d(d),h(h),s(s),state(state){}
    friend bool operator < (const record &p,const record &q){
        if(p.ss == q.ss){
            if(p.m == q.m){
                if(p.d == q.d){
                    if(p.h == q.h){
                        if(p.s == q.s){
                            return p.state > q.state;
                        }else{
                            return p.s < q.s;
                        }
                    }else{
                        return p.h < q.h;
                    }
                }else{
                    return p.d < q.d;
                }
            }else{
                return p.m < q.m;
            }
        }else{
            return p.ss < q.ss;
        }
    }
}res[1005];

double ans[35][70][70];   //存1日0时0分开始的花费

int main()
{
    for(int i = 1;i <= 24;++i){
        scanf("%d",&bill[i]);
    }
    
   //预处理ans数组
    int sum = 0;
    for(int i = 1;i <= 31;++i){
        for(int j = 0;j <= 23;++j){
            for(int k = 0;k <= 59;++k){
                ans[i][j][k] = sum * 0.01;
                sum += bill[j + 1];
            }
        }
    }
    //cout << ans[28][16][5] - ans[28][15][41] << endl;

    int n;
    scanf("%d",&n);
    string s;
    int pos;
    getchar();
    //输入处理到结构体中
    for(int i = 0;i < n;++i){
        getline(cin,s);
        string ss;
        int m,d,h,t,state;
        pos = s.find_first_of(' ',0);
        ss = s.substr(0,pos);
        int pos1 = s.find_first_of(' ',pos + 1);
        string p = s.substr(pos + 1,pos1 - pos - 1);
        m = (p[0] - '0') * 10 + p[1] - '0';
        d = (p[3] - '0') * 10 + p[4] - '0';
        h = (p[6] - '0') * 10 + p[7] - '0';
        t = (p[9] - '0') * 10 + p[10] - '0';
        p = s.substr(pos1 + 1,s.size() - pos1 - 1);
        if(p == "on-line"){
            state = 1;
        }else{
            state = 0;
        }
        res[i] = record(ss,m,d,h,t,state);
    }
    sort(res,res + n);
//    for(int i = 0;i < n;++i){
//        cout << res[i].ss << " " << res[i].m << " " << res[i].d << " " << res[i].h << " " << res[i].s << " " << res[i].state << endl;
//    }
    string lst;
    stack<record>sta;
    double ptr = 0;
    bool flag = false;
    for(int i = 0;i < n;++i){
        if(res[i].ss != lst){
            if(flag){
                printf("Total amount: $%.2lf\n",ptr);
                flag = false;
            }
            ptr = 0;
            while(!sta.empty()) sta.pop();
            if(res[i].state == 1){
                sta.push(res[i]);
            }
            lst = res[i].ss;
        }else if(res[i].state == 1){
            sta.push(res[i]);
        }else if(res[i].state == 0){
            if(sta.empty()){
                continue;
            }
            record tmp = sta.top();
            sta.pop();
            if(!flag){
                cout << res[i].ss << " ";
                printf("%02d\n",res[i].m);
                flag = true;
            }
            while(!sta.empty()) sta.pop();
            int x1 = tmp.d,y1 = tmp.h,z1 = tmp.s;
            int x2 = res[i].d,y2 = res[i].h,z2 = res[i].s;
            printf("%02d:%02d:%02d ",tmp.d,tmp.h,tmp.s);
            printf("%02d:%02d:%02d ",res[i].d,res[i].h,res[i].s);
            int pp = x2 * 24 * 60 + y2 * 60 + z2 - x1 * 24 * 60 - y1 * 60 - z1;
            printf("%d $%.2lf\n",pp,ans[x2][y2][z2] - ans[x1][y1][z1]);
            ptr += ans[x2][y2][z2] - ans[x1][y1][z1];
        }
    }
    if(flag){
        printf("Total amount: $%.2lf\n",ptr);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值