1016 Phone Bills

##### 9、电话账单✔
map里面放vector
y总计算费用(前缀和)好像确实比我方便很多。[我自己的](https://blog.csdn.net/hmsjdzzj/article/details/122911187)我写的什么JB,艹

//先将每个人的记录存储,然后排序进行筛选
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
#include<numeric>
#include<iomanip>
using namespace std;

vector<double> cost;
double day_cost;

struct Record{
    string time;
    string flag;
};

bool cmp(Record a,Record b){
    return a.time<b.time;
}

void charge_day(string begin,string end,int &time,double &charge){
    string beginh=begin.substr(0,2),endh=end.substr(0,2);
    begin=begin.substr(3);
    end=end.substr(3);
    if(beginh==endh){
        int temp=stoi(end)-stoi(begin);
        time+=temp;  //聊了30分钟
        charge+=cost[stoi(beginh)]*temp;
    }
    else{
        int h=stoi(beginh);
        time+=60-stoi(begin);
        charge+=cost[h]*(60-stoi(begin));
        h++;
        for(;h<stoi(endh);h++ ){
            time+=60;
            charge+=cost[h]*60;
        }
        time+=stoi(end);
        charge+=cost[h]*stoi(end);
    }
    
    
    
    
}

void charge(string begin,string end,double &total){  //这样有一个坏处,就是不能递归了
    int time=0;
    double charge=0;
    //先算天
    string beginday=begin.substr(0,2),endday=end.substr(0,2);
    begin=begin.substr(3);
    end=end.substr(3);
    if( begin<=end){
        time+=24*60*(stoi(endday)-stoi(beginday));
        charge+=day_cost*(stoi(endday)-stoi(beginday));
        charge_day(begin,end,time,charge);        
    }
    else if(begin>end){
        charge_day(end,begin,time,charge);
        time = 24*60-time;
        charge = day_cost-charge;
        time+=24*60*(stoi(endday)-stoi(beginday)-1);
        charge+=day_cost*(stoi(endday)-stoi(beginday)-1);
        
    }
    
    
    cout<<time<<" $"<<charge/100<<endl;
    total+=charge;
}

int main()
{
    cout<<setiosflags( ios::fixed )<<setprecision(2);
    //1 初始化cost
    for(int i=0;i<24;i++){
        double temp;
        cin>>temp;
        cost.push_back(temp);
    }
    day_cost=60*accumulate(cost.begin(),cost.end(),0);
    //2 构建存储结构
    int k;
    cin>>k;
    map<string,vector<Record> > data;
    //2.1 先储存信息 ,后计算总时间和价格       data[lili]=vector< 1/22 17.30         >
    for(int i=0;i<k;i++){
        string name;
        Record record;
        cin>>name>>record.time>>record.flag;
        // if( !data.count(name) ){
        //     vector<Record> ini;
        //     data[name]=ini;
        // } 
        data[name].push_back(record);
    }
    //2.2 排序
    for(auto it=data.begin();it!=data.end();it++){
        sort( (it->second).begin(),    (it->second).end(),cmp           );
    }
    //2.3 删除无效项
    for(auto it=data.begin();it!=data.end();it++){
        //对于每个name进行遍历
        for(auto itv=it->second.begin();itv!=it->second.end();){
            //成对的找,如果当前一对【1,2】不满足条件,删除1,继续找【2,3】
            if(   itv == it->second.end()-1   ) it->second.erase(itv);
            else if( !(itv->flag=="on-line" && (itv+1)->flag=="off-line")        ) it->second.erase(itv);
            else{
                itv+=2;
            }
        }

    }
    
    
    
    //二**直接对data中的数据进行加工输出即可*************************************************************************************
    
    for(auto it=data.begin();it!=data.end();it++){
        if(it->second.size()==0) continue;
        //输出每个人的标题
        string month= it->second[0].time.substr(0,2);
        cout<<it->first<<" "<<month<<endl;
        //逐项输出账单,并记录总账单。
        double total=0;
        for(auto itv=it->second.begin();itv!=it->second.end();){
            //获取前后时间
            string begin=itv->time.substr(3);
            itv++;
            string end=itv->time.substr(3);
            itv++;
            //统计信息
            cout<<begin<<" "<<end<<" ";
            charge(begin,end,total); //计算后函数中直接输出
        }
        //cout<<"Total amount: $"<<total/100<<endl;
        printf("Total amount: $%.2f", total/100.00);
        cout<<endl;
    }
    
    
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值