1016. Phone Bills (25)

1. 采用计算月初00:00:00到现在的分钟数,然后将算的钱差值得出,比直接时间差值再求钱数简单

2. 题目中只强调至少有一条匹配成功的记录,不是说每个人都至少有一条匹配成功的记录,所以没有成功匹配记录的人不输出


#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
#include <algorithm>

using namespace std;

struct Record{
	int month, date, hour, minute;
	int total;
	string line;

	Record(int month, int date, int hour, int minute, string s)
		: month(month), date(date), hour(hour), minute(minute), line(s){
			total = 24*60*date + 60*hour + minute;
		}

	bool operator < (const Record& rhs) const{
		return date < rhs.date
		|| (date == rhs.date && hour < rhs.hour)
		|| (date == rhs.date && hour == rhs.hour && minute < rhs.minute);
	}
};

vector<int> toll(24);

// calculate the cost from the month begin(cents)
int costByTime(int time){
	int hours = time / 60;
	int minutes = time % 60;
	int money = 0;

	for(int i = 0; i < hours; ++i){
		money += toll[i % 24] * 60;
	}

	money += toll[hours % 24] * minutes;

	return money;
}

// calculate the money of a pair of records(dollors)
double calcCost(const Record& begin, const Record& end){
	return (costByTime(end.total) - costByTime(begin.total)) / 100.0;
}

int main(){
	for(auto& n : toll){
		scanf("%d", &n);
	}

	int n;
	scanf("%d", &n);

	map<string, vector<Record>> calls;

	for(int i = 0; i < n; ++i){
		string name, line;
		int month, date, hour, minute;

		cin >> name;
		scanf("%d%*c%d%*c%d%*c%d", &month, &date, &hour, &minute);
		cin >> line;

		auto it = calls.find(name);
		if(it == calls.end()){
			calls[name] = vector<Record>();
		}

		calls[name].emplace_back(month, date, hour, minute, line);
	}

	for(auto it = calls.begin(); it != calls.end(); ++it){
		sort(it->second.begin(), it->second.end());
		vector<Record> records;

		for(size_t i = 0; i + 1 < it->second.size(); i += 2){
			auto r1 = it->second[i], r2 = it->second[i + 1];

			if(r1.line == "on-line" && r2.line == "off-line"){
				records.push_back(r1);
				records.push_back(r2);
			}else{
				--i;
			}
		}

		it->second = records;
	}

	for(auto it = calls.begin(); it != calls.end(); ++it){
		if(it->second.empty()) continue;

		printf("%s %02d\n", it->first.c_str(), it->second.front().month);

		double sum = 0;
		for(size_t i = 0; i + 1 < it->second.size(); i += 2){
			auto r1 = it->second[i], r2 = it->second[i + 1];
			double cost = calcCost(r1, r2);

			printf("%02d:%02d:%02d %02d:%02d:%02d %d $%0.2f\n", r1.date, r1.hour, r1.minute,
				r2.date, r2.hour, r2.minute, r2.total - r1.total, cost);
			sum += cost;
		}

		printf("Total amount: $%0.2f\n", sum);
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值