【题解搬运】PAT_A1016 Phone Bills

来源:互联网 发布:易语言钓鱼源码大全 编辑:程序博客网 时间:2024/05/19 01:09

从我原来的博客上搬运。原先blog作废。

题目

A long-distance telephone company charges its customers by the following rules:

Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a long-distance call, the time will be recorded, and so will be the time when the customer hangs up the phone. Every calendar month, a bill is sent to the customer for each minute called (at a rate determined by the time of day). Your job is to prepare the bills for each month, given a set of phone call records.

题解

繁琐,但是不难。合理运用封装能够减少代码量。
剩下的是两个注意事项:1.是连续的on-off算作一个记录。一个例子:
01 on 02 on 03 off 04 off 那么02-03算作一个记录。
2.必须要注意这个情况,就是如果没有合法的记录的话就不打印账单!!!不论是一开始的还是tot Amount!我第二个一开始忘了不打印了!!
以上。

代码

#include <bits/stdc++.h>using namespace std;#define f1(x,y) for(int x=1;x<=y;++x)#define f0(x,y) for(int x=0;x!=y;++x)#define bf1(x,y,z) for(int x=y;x>=z;--x)#define bf0(x,y,z) for(int x=y;x!=z;--x)typedef long long ll;typedef unsigned long long ull;int fee[25];int ohrfee[25]={0};//fee of 00:00~i:00int dayfee=0;struct Record{    int month;    int recMin;    bool isOn;    Record(int _m=1,int _r=0,bool _o=true):        month(_m),recMin(_r),isOn(_o)        {}    bool operator < (const Record& rhs) const    {        return recMin<rhs.recMin;    }};typedef pair<Record,Record> Call;struct User{    string name;    int month;    vector<Record> rec;    vector<Call> cal;    User(string _n):name(_n) {}    void calc()    {        sort(rec.begin(),rec.end());        int marked[1005];        memset(marked,-1,sizeof(marked));        month=rec[0].month;        bool isOk=false;        f0(i,rec.size()) if(rec[i].isOn && i+1<rec.size() && !(rec[i+1].isOn))        {            cal.push_back(make_pair(rec[i],rec[i+1]));        }        return;    }};vector<User> u;map<string,int> idx;int main(){    int f;    f0(i,24)    {        scanf("%d",&f);        fee[i]=f;        if(i==0) ohrfee[i]=0;        else ohrfee[i]=ohrfee[i-1]+fee[i-1]*60;        dayfee+=fee[i]*60;    }    int n; scanf("%d",&n);    f1(i,n)    {        char name[25],status[10];        int mon,d,h,minute;        scanf("%s %d:%d:%d:%d %s",name,&mon,&d,&h,&minute,status);        if(idx.find(string(name))==idx.end())        {            u.push_back(User(string(name)));            idx[string(name)]=u.size()-1;            u.back().rec.push_back(Record(mon,d*1440+h*60+minute,status[1]=='n'));        }        else u[idx[string(name)]].rec.push_back(Record(mon,d*1440+h*60+minute,status[1]=='n'));    }    for(auto iter=u.begin();iter!=u.end();++iter) iter->calc();    for(auto iter=idx.begin();iter!=idx.end();++iter)    {        int num=iter->second;        string nam=iter->first;        User& usr=u[num];        vector<Call>& c=usr.cal;        if(c.size()!=0) printf("%s %02d\n",nam.c_str(),usr.month);        int tot=0;//100cents->1$        f0(i,c.size())        {            int fd,fh,fm,sd,sh,sm,cost;            fd=c[i].first.recMin/1440;fh=(c[i].first.recMin%1440)/60;fm=(c[i].first.recMin%60);            sd=c[i].second.recMin/1440;sh=(c[i].second.recMin%1440)/60;sm=(c[i].second.recMin%60);            cost=(sd-fd)*dayfee+(ohrfee[sh]+sm*fee[sh])-(ohrfee[fh]+fm*fee[fh]);            //printf("2:ohrfee[%d]=%d,sm=%d,fee[%d]=%d.\n",sh,ohrfee[sh],sm,sh,fee[sh]);            //printf("1:ohrfee[%d]=%d,sm=%d,fee[%d]=%d.\n",fh,ohrfee[fh],fm,fh,fee[fh]);            printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",fd,fh,fm,                                                              sd,sh,sm,                                                              c[i].second.recMin-c[i].first.recMin,cost/100.0);            tot+=cost;        }        if(c.size()!=0) printf("Total amount: $%.2f\n",tot/100.0);    }    return 0;}