PAT1016. Phone Bills

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<iostream>
using namespace std;
int rate[25];
int N;
struct cut
{
    char name[25];
    char date[15];
    int month;
    int day;
    int hour;
    int minute;
    int state;//on||off
}cus[1005];
vector<cut> Call;
int calltime(cut a,cut b)
{
    int atime=a.day*24*60+a.hour*60+a.minute;
    int btime=b.day*24*60+b.hour*60+b.minute;
    return btime-atime;
}
long charge(cut a)
{
    long amoney=0;//
    amoney+=a.day*60*rate[24];
    int sumrate=0;
    for(int i=0;i<a.hour;i++)
    {
        sumrate+=rate[i];
    }
    amoney+=sumrate*60;
    amoney+=rate[a.hour]*a.minute;
    return amoney;
}
double callmoney(cut a,cut b)
{
    //printf("test=%ld\n",charge(b)-charge(a));
    return (charge(b)-charge(a))*1.0/100;
}
int main()
{
    for(int i=0;i<24;i++){
        scanf("%d",&rate[i]);
        rate[24]+=rate[i];
    }
    //printf("%d\n",rate[24]);
    scanf("%d",&N);
    for(int i=0;i<N;i++){
        char  state[10];
        scanf("%s %s %s",cus[i].name,cus[i].date,state);
        cus[i].state=(strcmp(state,"on-line")==0)?0:1;
        sscanf(cus[i].date,"%d:%d:%d:%d",
               &cus[i].month,&cus[i].day,&cus[i].hour,&cus[i].minute);
    }
    int cmp(cut a,cut b);
    sort(cus,cus+N,cmp);
    //del error calls
    bool isonline=false;
    string name;
    for(int i=0;i<N;i++)
    {
        if(isonline==false&&cus[i].state==0)
        {
            Call.push_back(cus[i]);
            isonline=true;
            name=cus[i].name;
        }
        else if(isonline==true&&cus[i].state==0)
        {
            Call.pop_back();
            Call.push_back(cus[i]);
            isonline=true;
            name=cus[i].name;
        }
        else if(isonline==true&&cus[i].state==1&&cus[i].name==name)
        {
            Call.push_back(cus[i]);
            isonline=false;
        }
    }
    //the last
    if((*(Call.end()-1)).state==0)
    {
        Call.pop_back();
    }
    //output
    double total=0;
    name="";
    for(int i=0;i<Call.size();i+=2)
    {
        if(name!=Call[i].name){
            if(name!="")
            {
                printf("Total amount: $%.2lf\n",total);
                total=0;
                printf("%s %02d\n",Call[i].name,Call[i].month);
            }
            else{
                printf("%s %02d\n",Call[i].name,Call[i].month);
            }
            name=Call[i].name;//new customer name
        }
        printf("%02d:%02d:%02d ",Call[i].day,Call[i].hour,Call[i].minute);
        printf("%02d:%02d:%02d ",Call[i+1].day,Call[i+1].hour,Call[i+1].minute);
        printf("%d ",calltime(Call[i],Call[i+1]));
        printf("$%.2lf\n",callmoney(Call[i],Call[i+1]));
        total+=callmoney(Call[i],Call[i+1]);
    }
    printf("Total amount: $%.2lf\n",total);
    return 0;
}
int cmp(cut a,cut b)
{
    if(strcmp(a.name,b.name)!=0)
    {
        return strcmp(a.name,b.name)<0;
    }
    else
    {
            return strcmp(a.date,b.date)<0;
    }
}
相關文章
相關標籤/搜索