zju pat 1016. Phone Bills (25)

#include <iostream>
#include <string>
#include <math.h>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
#define INF 0xeffffff
typedef struct Phone_Record
{
    string name;
    int month;
    int day;
    int hour;
    int minute;
    string status;
    int total;
};

bool cpm(Phone_Record c1,Phone_Record c2)
{
    if(c1.name < c2.name) 
        return true;
    else if(c1.name == c2.name && c1.total < c2.total)
        return true;
    return false;
}

int toll[24];

int charge_by_time(int total) //从起点开始算
{
    int hours = total/60;
    int minutes = total%60;
    int money =0;
    int i ;
    for( i =0 ; i <hours;i++)
        money += toll[i%24]*60;
    money += toll[i%24]*minutes;
    return money;
}

double cal_cost(Phone_Record c1,Phone_Record c2)
{
    return (double)abs((charge_by_time(c1.total) - charge_by_time(c2.total)))/100 ; //转化到美元
}

int call_lasting_time(Phone_Record c1,Phone_Record c2) //电话持续时间
{
    return abs(c1.total - c2.total);
}

int main()
{
    char temp_time[50];
    int n,m,k,q;
    int i,j;
    for(i = 0;i<24;i++)
        scanf("%d",&toll[i]);
    scanf("%d",&n);
    vector<Phone_Record>call;
    vector<Phone_Record>good_call;
    Phone_Record c;
    for(i =0;i<n;i++)
    {
        cin>>c.name;
        scanf("%d:%d:%d:%d",&c.month,&c.day,&c.hour,&c.minute);
        cin>>c.status;
        c.total = c.day *60*24 + c.hour *60 + c.minute;  //利用总时间,来简化比较
        call.push_back(c);
    }
    sort(call.begin(),call.end(),cpm);  //排好序,方便对于同一用户比较
    bool online = false;
    string cur_name;
    for(i =0;i<n;i++) //过滤不符合逻辑的情况
    {
        
        if(online == false && call[i].status == "on-line")  //保存在线的
        {
            good_call.push_back(call[i]);
            online = true;
            cur_name = call[i].name;
        }
        else if(online == true && call[i].status == "off-line" && cur_name == call[i].name)//前面是在线,现在是挂断并且是同一个人
        {
            good_call.push_back(call[i]);
            online = false;
            cur_name = call[i].name;
        }
        else if(online == true && call[i].status == "on-line") //两个相邻的在线,去除前面那个
        {
            good_call.pop_back();
            good_call.push_back(call[i]);
            online = true;
            cur_name = call[i].name;
        }
    }
    if((*(good_call.end()-1)).status == "on-line")
        good_call.pop_back();
    
    double totalcost =0;
    string curname = "";
     for(int i=0;i<good_call.size();i+=2)  
    {  
  
        if(good_call[i].name != curname)  //名字不一样的情况
        {  
            if(curname!="")  
            {  
                printf("Total amount: $%.2f
",totalcost);  
                totalcost = 0;  
                printf("%s %02d
",good_call[i].name.c_str(),good_call[i].month);  
            }  
            else //第一次进来 
            {  
                printf("%s %02d
",good_call[i].name.c_str(),good_call[i].month);  
            }  
            curname = good_call[i].name;  
        }  
        printf("%02d:%02d:%02d",good_call[i].day,good_call[i].hour,good_call[i].minute);  
        printf(" ");  
        printf("%02d:%02d:%02d",good_call[i+1].day,good_call[i+1].hour,good_call[i+1].minute);  
        printf(" ");  
        printf("%d",call_lasting_time(good_call[i],good_call[i+1]));  
        printf(" ");  
        printf("$%.2f
",cal_cost(good_call[i],good_call[i+1]));  
        totalcost+=cal_cost(good_call[i],good_call[i+1]);  
    }  
    printf("Total amount: $%.2f
",totalcost);
//    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/cheng07045406/p/3519924.html