PAT 甲级 1016 Phone Bills

遇到一个坑点,没有有效通话记录的顾客什么都不打印
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

int rates[24];

struct Record
{
	char name[30], status[20];
	int month, day, hour, min;
};

bool cmp(const Record &r1, const Record &r2)
{
	if(strcmp(r1.name, r2.name) != 0)
		return strcmp(r1.name, r2.name) < 0;
	else if(r1.month != r2.month)
		return r1.month < r2.month;
   	else if(r1.day != r2.day)
		return r1.day < r2.day;
	else if(r1.hour != r2.hour)
		return r1.hour < r2.hour;
	else
		return r1.min < r2.min;
}

double calCost(Record &r1, Record &r2)
{
	double cost = 0;
	int time[24]; memset(time, 0, sizeof(time));
	if(r1.day != r2.day) // 不是同一天
	{
		for(int i = 0; i < 24; i++)
			time[i] += (r2.day-r1.day-1) * 60;
		time[r1.hour] += 60 - r1.min;
		for(int i = r1.hour+1; i < 24; i++)
			time[i] += 60;
		for(int i = 0; i < r2.hour; i++)
			time[i] += 60;
		time[r2.hour] += r2.min;
	}
	else if(r1.hour != r2.hour) // 同一天不同小时
	{
		time[r1.hour] += 60 - r1.min;
		for(int i = r1.hour+1; i < r2.hour; i++)
			time[i] += 60;
		time[r2.hour] += r2.min;
	}
	else 
		time[r1.hour] += r2.min - r1.min; // 同一天同一小时	
	for(int i = 0; i < 24; i++)
		cost += rates[i]*time[i];
	return cost/100;
}

int calTime(Record &r1, Record &r2)
{
	return (r2.day-r1.day)*24*60 + (r2.hour-r1.hour)*60 + (r2.min-r1.min);
}

struct Customer
{
	char name[30];
	int month; double cost;
	queue<Record> on_lineRecords, off_lineRecords;
}customer;

void PrintBill(Customer &customer)
{
	bool flag = true;
	queue<Record> &on_line = customer.on_lineRecords, &off_line = customer.off_lineRecords;
	// cout << on_line.size() << "  " << off_line.size() << endl;
	Record r1, r2; int duration, cnt = 0; double cost;
	while(!on_line.empty() && !off_line.empty())
	{
		if(cmp(off_line.front(), on_line.front()))
		{
			off_line.pop();
			continue;
		}
		r2 = off_line.front();
		off_line.pop();
		do
		{
			r1 = on_line.front(); 
			on_line.pop();
		} while(!on_line.empty() && cmp(on_line.front(), r2) );
		if(flag)
		{
			printf("%s %02d\n", customer.name, customer.month);
			flag = false;
		}
		cost = calCost(r1, r2); duration = calTime(r1, r2);
		printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n", r1.day, r1.hour, r1.min,
			r2.day, r2.hour, r2.min, duration, cost);
		customer.cost += cost; cnt++;
	}
	while(!on_line.empty())
		on_line.pop();
	while(!off_line.empty())
		off_line.pop();
	if(cnt)
		printf("Total amount: $%.2f\n", customer.cost);
}

int main()
{
	// freopen("1016.data", "r", stdin);
	// 读取费率
	for(int i = 0; i < 24; i++)
		scanf("%d", rates+i);
	// 读取通话记录
	int n; scanf("%d", &n);
	vector<Record> records(n);
	for(int i = 0; i < n; i++)
		scanf("%s %d:%d:%d:%d %s", records[i].name, &records[i].month,
			&records[i].day, &records[i].hour, &records[i].min, records[i].status);
	// 将通话记录先按时间顺序排序,再按姓名排序
	stable_sort(records.begin(), records.end(), cmp);
	// 按姓名顺序整理每个人的通话记录并打印账单
	vector<Record>::iterator it = records.begin();
	strcpy(customer.name, it->name); customer.month = it->month; customer.cost = 0;
	for(it = records.begin(); it != records.end(); it++)
	{
		if(strcmp(customer.name, it->name) != 0)
		{
			PrintBill(customer);
			strcpy(customer.name, it->name); customer.month = it->month; customer.cost = 0;
		}
		if(strcmp(it->status, "on-line") == 0)
			customer.on_lineRecords.push(*it);
		else if(strcmp(it->status, "off-line") == 0)
			customer.off_lineRecords.push(*it);
	}
	PrintBill(customer);
}

猜你喜欢

转载自blog.csdn.net/JueChenYi/article/details/88066177
今日推荐