1016 Phone Bills (25 分)电话费计算

本文详细介绍了如何设计一个电话计费系统,该系统能够根据不同的时间费率计算长途电话费用,并生成每月账单。通过解析输入文件,理解费率结构和电话记录,文章提供了具体的算法步骤,包括记录排序、费用计算和账单输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A long-distance telephone company charges its customers by the following rules:
Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a long-distance call, the time will be recorded, and so will be the time when the customer hangs up the phone. Every calendar month, a bill is sent to the customer for each minute called (at a rate determined by the time of day). Your job is to prepare the bills for each month, given a set of phone call records.

Input Specification:

Each input file contains one test case. Each case has two parts: the rate structure, and the phone call records.
The rate structure consists of a line with 24 non-negative integers denoting the toll (cents/minute) from 00:00 - 01:00, the toll from 01:00 - 02:00, and so on for each hour in the day.
The next line contains a positive number N (≤1000), followed by N lines of records. Each phone call record consists of the name of the customer (string of up to 20 characters without space), the time and date (mm:dd:hh:mm), and the word on-line or off-line.
For each test case, all dates will be within a single month. Each on-line record is paired with the chronologically next record for the same customer provided it is an off-line record. Any on-line records that are not paired with an off-line record are ignored, as are off-line records not paired with an on-line record. It is guaranteed that at least one call is well paired in the input. You may assume that no two records for the same customer have the same time. Times are recorded using a 24-hour clock.

Output Specification:

For each test case, you must print a phone bill for each customer.
Bills must be printed in alphabetical order of customers’ names. For each customer, first print in a line the name of the customer and the month of the bill in the format shown by the sample. Then for each time period of a call, print in one line the beginning and ending time and date (dd:hh:mm), the lasting time (in minute) and the charge of the call. The calls must be listed in chronological order. Finally, print the total charge for the month in the format shown by the sample.

一、按照姓名优先,日期次之的顺序对输入记录进行排序。

二、遍历记录,对于属于一对儿记录的,进行相应的计算以及输出。

1、每次通话费用的计算:分别计算出开始时间和结束时间到0天0时0分的费用,然后做差。
2、为了便于total amount的输出(需要考虑最后一条记录能否配对的问题),在排序后的记录中插入一个辅助记录,名字为空,这样只要遇到前后名字不一样的两条记录并且total amount值不为0,就可以输出上一个人的总花费。

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
struct input {
	string name, state;
	int month, day, hour, min;
};
bool cmp(input i1, input i2) {//先将人排序,然后将同一个人的每条记录排序
	if (i1.name != i2.name)return i1.name < i2.name;
	return i1.day != i2.day ? i1.day < i2.day :
		i1.hour != i2.hour ? i1.hour < i2.hour : i1.min < i2.min;
}
float cost[24], sumDay = 0;//分别记录每小时花费、24小时花费
float getCost(input i1, input i2,int &tMin) {
	float cost1, cost2;
	int i = 0;
	cost1 = i1.day * sumDay;
	for (i = 0; i < i1.hour; i++)cost1 += 60 * cost[i];
	cost1 += i1.min * cost[i];
	cost2 = i2.day * sumDay;
	for (i = 0; i < i2.hour; i++)cost2 += 60 * cost[i];
	cost2 += i2.min * cost[i];
	tMin = 24 * 60 * (i2.day - i1.day) + 60 * (i2.hour - i1.hour) + (i2.min - i1.min);
	return cost2 - cost1;
}
int main() {
	for (int i = 0; i < 24; i++) {
		cin >> cost[i];
		sumDay += cost[i] * 60;
	}
	int n;
	cin >> n;
	vector<input>peos;
	for (int i = 0; i < n; i++) {
		string name, state;
		int m, d, h, min;
		cin >> name;
		scanf("%d:%d:%d:%d", &m, &d, &h, &min);
		cin >> state;
		peos.push_back({ name,state,m,d,h,min });
	}
	sort(peos.begin(), peos.end(), cmp);
	input aux; //辅助记录
	aux.name = "";
	peos.push_back(aux);
	int flag = 0, pre = 0;
	float totalA = 0.0;//每一个人的总费用
	for (int i = 1; i < peos.size(); i++) {
		if (peos[pre].name == peos[i].name && peos[pre].state == "on-line" && peos[i].state == "off-line") {
			if (flag == 0) {//在第一条记录之前需要输出姓名和月份
				flag = 1;
				cout << peos[pre].name;
				printf(" %02d\n", peos[pre].month);
			}
			int totalM = 0;//每次通话时长
			float curCost = getCost(peos[pre], peos[i], totalM);//每次通话费用
			totalA += curCost;
			printf("%02d:%02d:%02d %02d:%02d:%02d", peos[pre].day, peos[pre].hour, peos[pre].min, peos[i].day, peos[i].hour, peos[i].min);
			printf(" %d $%0.2f\n", totalM, curCost/100.0);
		}
		else if ((peos[pre].name != peos[i].name)&&totalA > 0.001) {
			printf("Total amount: $%0.2f\n", totalA/100.0);
			totalA = 0;
			flag = 0;
		}
		pre = i;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值