1016. Phone Bills (25)

来源:互联网 发布:java代码实现单例模式 编辑:程序博客网 时间:2024/04/30 03:29
#include<iostream>#include<vector>#include<string>#include<iomanip>#include<algorithm>using namespace std;double T[24];int tt;double m;typedef struct{    int month;    int day;    int hour;    int min;    int kind;//kind=0,on-line;kind=1,off-line}ttime;typedef struct{    vector<ttime> vec;    string name;    double money;}customer;bool compare_customer(customer x,customer y){    if(x.name.compare(y.name)>0) return false;    else return true;}bool compare_ttime(ttime x,ttime y){    if(x.month <y.month || (x.month==y.month &&x.day<y.day) ||(x.month==y.month&&x.day==y.day&&x.hour<y.hour)||(x.month==y.month && x.day==y.day&&x.hour==y.hour&&x.min<y.min)) return true;    else return false;}void calculate(ttime t1,ttime t2){    if(t1.day==t2.day && t1.hour==t2.hour)     {        tt+=t2.min-t1.min;        m+=T[t1.hour]*(t2.min-t1.min);    }    if(t1.day==t2.day &&t1.hour<t2.hour)    {        tt+=60-t1.min+t2.min;        m+=(60-t1.min)*T[t1.hour]+t2.min*T[t2.hour];        for(int t=t1.hour+1;t<t2.hour;t++)        {            tt+=60;            m+=T[t]*60;        }    }    if(t1.day<t2.day)    {        ttime temp;        temp.day=t1.day;temp.hour=23;temp.min=60;        calculate(t1,temp);        temp.day=t2.day;temp.hour=0;temp.min=0;        calculate(temp,t2);        for(int t=t1.day+1;t<t2.day;t++)            for(int i=0;i<24;i++)            {                tt+=60;                m+=T[i]*60;            }    }}void Print(customer x){    int t=0;    while(t!=x.vec.size()-1)    {        if(x.vec[t].kind==0 && x.vec[t+1].kind==1) break;        t++;    }    if(t==x.vec.size()-1) return;    cout<<x.name<<" "<<setw(2)<<setfill('0')<<x.vec[t].month<<endl;    while(t<x.vec.size()-1)    {        tt=0;m=0.0;        if(x.vec[t].kind==0 && x.vec[t+1].kind==1)         {            calculate(x.vec[t],x.vec[t+1]);            cout<<setw(2)<<setfill('0')<<x.vec[t].day<<":"<<setw(2)<<setfill('0')<<x.vec[t].hour<<":"<<setw(2)<<setfill('0')<<x.vec[t].min<<" "<<setw(2)<<setfill('0')<<x.vec[t+1].day<<":"<<setw(2)<<setfill('0')<<x.vec[t+1].hour<<":"<<setw(2)<<setfill('0')<<x.vec[t+1].min<<" "<<tt<<" $"<<setprecision(2)<<fixed<<m<<endl;            x.money+=m;            t+=2;continue;        }        t++;    }    cout<<"Total amount: $"<<setprecision(2)<<fixed<<x.money<<endl;}int main(){    vector<customer> cus;    for(int t=0;t<24;t++)    {        cin>>T[t];        T[t]=T[t]/100;    }    int num;    cin>>num;    int x1,x2,x3,x4;    string str1,str2;    for(int t=0;t<num;t++)    {        customer ctem;        ttime ttem;        cin>>str1;        scanf("%d:%d:%d:%d",&x1,&x2,&x3,&x4);        cin>>str2;        ttem.month=x1;        ttem.day=x2;        ttem.hour=x3;        ttem.min=x4;        if(str2.compare("on-line")==0) ttem.kind=0;        else ttem.kind=1;        vector<customer>::iterator it=cus.begin();        for(;it!=cus.end();it++)            if(it->name.compare(str1)==0)            {                it->vec.push_back(ttem);                break;            }        if(it==cus.end())        {            ctem.name=str1;            ctem.vec.push_back(ttem);            ctem.money=0;            cus.push_back(ctem);        }    }    sort(cus.begin(),cus.end(),compare_customer);    for(vector<customer>::iterator t=cus.begin();t!=cus.end();t++)    {        sort((t)->vec.begin(),(t)->vec.end(),compare_ttime);        Print(*t);    }}
0 0