PAT甲级1016. Phone Bills

PAT甲级1016. Phone Bills

题意:

长途电话公司按以下规定向客户收取费用:

长途电话费用每分钟一定数量,具体取决于通话时间。当客户开始连接长途电话时,将记录时间,并且客户挂断电话时也是如此。
每个日历月份,每分钟都会向客户发送一个帐单(按照一天中的时间确定)。您的工作是为每个月准备账单,给出一组电话记录。

输入:

每个输入文件包含一个测试用例。每个案例有两部分:费率结构和电话记录。
费率结构包括一个24个非负整数表示从00:00至01:00的收费(分/分),01:00至02:00之间的收费,以及当天每小时的费用。

下一行包含正数N(<= 1000),后跟N行记录。
每个电话记录包括客户名称(最多20个字符的空格字符串),时间和日期(mm:dd:hh:mm)和单词“在线”或“离线” 。

对于每个测试用例,所有日期将在一个月内。每个“
线“记录与同一客户的时间顺序的下一条记录配对,只要它是一个”离线“记录,任何不与”离线“记录配对的”在线“记录将被忽略,”离线“记录未与”在线“记录配对,保证在输入中至少有一个呼叫配对良好。
您可以假设同一客户没有两个记录有相同的时间。使用24小时制记录时间。

输出:

对于每个测试用例,您必须为每个客户打印一个电话费。

票据必须按客户姓名的字母顺序打印。对于每个客户,
首先按照示例显示的格式,一行列出客户的名称和帐单的月份。然后,对于每个呼叫的时间段,在一行中打印开始和结束时间和日期(dd:hh:mm),持续时间(分钟)和通话费用。呼叫必须按时间顺序列出。最后,
以样品显示的格式打印月份的总费用。

思路:

这题很烦。
把所有的通话记录排序。只要第i个是online i + 1是offline就是有效的。
有个地方特别坑,就是一个用户要是没有任何有效的通话记录的话,就什么都不输出。连名字,total = 0都不输出。哎。不消费的话,什么都不给你输出。万恶的资本主义。别的地方注意一下细节就好了。

ac代码:

C++

// pat1016.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<cstring>
#include<unordered_map>
#include<map>

using namespace std;

int charge[24];

struct bill
{
	string id;
	vector<pair<int,string>> line;
	vector<pair<string, string>> vaild;
	vector<int> costtime;
	vector<double> cost;
};

static bool cmp(pair<int,string> a, pair<int, string> b)
{
	int amonth, adata, ahour, aminute;
	int bmonth, bdata, bhour, bminute;
	amonth = stoi(a.second.substr(0, 2));
	adata = stoi(a.second.substr(3, 2));
	ahour = stoi(a.second.substr(6, 2));
	aminute = stoi(a.second.substr(9, 2));
	bmonth = stoi(b.second.substr(0, 2));
	bdata = stoi(b.second.substr(3, 2));
	bhour = stoi(b.second.substr(6, 2));
	bminute = stoi(b.second.substr(9, 2));
	
	if (amonth != bmonth)
	{
		return amonth < bmonth;
	}
	else if (adata != bdata)
	{
		return adata < bdata;
	}
	else if(ahour != bhour)
	{
		return ahour < bhour;
	}
	else if (aminute != bminute)
	{
		return aminute < bminute;
	}
	return true;
}

int counttime(string a, string b)
{
	int adata, ahour, aminute;
	int bdata, bhour, bminute;
	adata = stoi(a.substr(3, 2));
	ahour = stoi(a.substr(6, 2));
	aminute = stoi(a.substr(9, 2));
	bdata = stoi(b.substr(3, 2));
	bhour = stoi(b.substr(6, 2));
	bminute = stoi(b.substr(9, 2));

	int res;
	res = (bdata - adata) * 24 * 60 + (bhour - ahour) * 60 + (bminute - aminute);
	return res;
}

double count(string a, string b)
{
	int adata, ahour, aminute;
	int bdata, bhour, bminute;
	adata = stoi(a.substr(3, 2));
	ahour = stoi(a.substr(6, 2));
	aminute = stoi(a.substr(9, 2));
	bdata = stoi(b.substr(3, 2));
	bhour = stoi(b.substr(6, 2));
	bminute = stoi(b.substr(9, 2));

	double res = 0;
	int j;
	for (int i = adata; i <= bdata; i++)
	{
		if (i < bdata)
		{
			for (j = ahour; j < 24; j++)
			{
				res += (60 - aminute) * charge[j];
				aminute = 0;
			}
			ahour = 0;
		}
		else if (i == bdata)
		{
			for (j = ahour; j <= bhour; j++)
			{
				if (j < bhour)
				{
					res += (60 - aminute) * charge[j];
					aminute = 0;
				}
				else if (j == bhour)
				{
					res += (bminute - aminute) * charge[j];
					break;
				}
			}
		}
	}
	return res / 100;
}

void findvaild(bill& b)
{
	sort(b.line.begin(), b.line.end(), cmp);
	int len = b.line.size();
	for (int i = 0; i < len - 1; i++)
	{
		if (b.line[i].first == 0 && b.line[i + 1].first == 1)
		{
			b.vaild.push_back(make_pair(b.line[i].second.substr(3), b.line[i + 1].second.substr(3)));
			b.costtime.push_back(counttime(b.line[i].second, b.line[i + 1].second));
			b.cost.push_back(count(b.line[i].second, b.line[i + 1].second));
			i++;
		}
	}
}

int main()
{
	int n;
	map<string,bill> bl;
	vector<string> allid;
	//收费时段记录
	for (int i = 0; i < 24; i++)
		cin >> charge[i];

	//通话记录
	string id, time, type;
	string month;
	cin >> n;

	while (n--)
	{
		cin >> id >> time >> type;
		if (bl.find(id) == bl.end())
		{
			bill b;
			b.id = id;
			bl[id] = b;
			allid.push_back(id);
		}

		if (type == "on-line")
		{
			bl[id].line.push_back(make_pair(0,time));
		}
		else
		{
			bl[id].line.push_back(make_pair(1, time));
		}
	}

	month = time.substr(0, 2);
	//处理记录
	sort(allid.begin(), allid.end());
	for (int i = 0; i < allid.size(); i++)
	{
		findvaild(bl[allid[i]]);
		if (bl[allid[i]].vaild.size() == 0) continue;
		double total = 0;
		cout << bl[allid[i]].id << " " << month << endl;
		for (int j = 0; j < bl[allid[i]].vaild.size(); j++)
		{
			total += bl[allid[i]].cost[j];
			cout << bl[allid[i]].vaild[j].first << " " << bl[allid[i]].vaild[j].second << " " << bl[allid[i]].costtime[j] << " $";
			printf("%.2f
", bl[allid[i]].cost[j]);
		}
		printf("Total amount: $%.2f
", total);
	}
    return 0;
}


原文地址:https://www.cnblogs.com/weedboy/p/7252869.html