A1016 Phone Bills (25 分)

前一天晚上基本完全照抄晴神宝典的代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int toll[25];
struct record
{
	char id[25];
	int mm1, dd, hh, mm2;
	bool s;//状态on/off
}cust[1010], temp1, temp2;
void cal(int on, int off, int& time, int& money)//计费
{
	temp1 = cust[on];
	temp2 = cust[off];
	while (temp1.dd < temp2.dd || temp1.hh < temp2.hh || temp1.mm2 < temp2.mm2)
	{
		time++;
		money += toll[temp1.hh];//资费
		temp1.mm2++;
		if (temp1.mm2 >= 60)
		{
			temp1.mm2 = 0;
			temp1.hh++;
		}
		if (temp1.hh >= 24)
		{
			temp1.hh = 0;
			temp1.dd++;
		}
	}
}
bool cmp(record a, record b)
{
	if (strcmp(a.id, b.id) != 0) return strcmp(a.id, b.id) < 0;
	else if (a.mm1 != b.mm1) return a.mm1 < b.mm1;
	else if (a.dd != b.dd) return a.dd < b.dd;
	else if (a.hh != b.hh) return a.hh < b.hh;
	else if (a.mm2 != b.mm2) return a.mm2 < b.mm2;
}
int main()
{
	int n;
	char line[15];//暂存on/off
	for (int i = 0; i < 24; i++)
	{
		scanf("%d", &toll[i]);
	}
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%s", cust[i].id);
		scanf("%d:%d:%d:%d", &cust[i].mm1, &cust[i].dd, &cust[i].hh, &cust[i].mm2);
		scanf("%s", line);
		if (strcmp(line,"on-line" ) == 0) cust[i].s = 1;
		else cust[i].s = 0;
	}
	sort(cust, cust + n, cmp);

	int on = 0, off, next;
	while (on < n)
	{
		next = on;
		int np = 0;
		while (next < n&&strcmp(cust[on].id, cust[next].id) == 0)
		{
			if (np == 0 && cust[next].s == 1) np = 1;
			else if (np == 1 && cust[next].s == 0) np = 2;
			next++;
		}
		if (np < 2)
		{
			on = next;
			continue;
		}
		printf("%s %02d\n", cust[on].id, cust[on].mm1);
		int total = 0;
		while (on < next)
		{
			while (on < next - 1 && !(cust[on].s == 1 && cust[on + 1].s == 0))
			{
				on++;
			}
			off = on + 1;
			if (off == next)
			{
				on = next;
				break;
			}
			printf("%02d:%02d:%02d ", cust[on].dd, cust[on].hh, cust[on].mm2);
			printf("%02d:%02d:%02d ", cust[off].dd, cust[off].hh, cust[off].mm2);
			int money = 0, time = 0;
			cal(on, off, time, money);
			total += money;
			printf("%d $%.2f\n", time, money / 100.0);
			on = off + 1;
		}
		printf("Total amount:$%.2f\n", total / 100.0);
	}
	return 0;
}


后来总觉得在sort之后的输出有些和我波长对不上(大概是新手不太熟练的缘故),然后按照我的直线思维写了一下在排序后的输出:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int toll[25];
struct record
{
	char id[25];
	int mm1, dd, hh, mm2;
	bool s;//状态on/off
}cust[1010], temp1, temp2;
void cal(int on, int off, int& time, int& money)//计费
{
	temp1 = cust[on];
	temp2 = cust[off];
	while (temp1.dd < temp2.dd || temp1.hh < temp2.hh || temp1.mm2 < temp2.mm2)
	{
		time++;
		money += toll[temp1.hh];//资费
		temp1.mm2++;
		if (temp1.mm2 >= 60)
		{
			temp1.mm2 = 0;
			temp1.hh++;
		}
		if (temp1.hh >= 24)
		{
			temp1.hh = 0;
			temp1.dd++;
		}
	}
}
bool cmp(record a, record b)
{
	if (strcmp(a.id, b.id) != 0) return strcmp(a.id, b.id) < 0;
	else if (a.mm1 != b.mm1) return a.mm1 < b.mm1;
	else if (a.dd != b.dd) return a.dd < b.dd;
	else if (a.hh != b.hh) return a.hh < b.hh;
	else if (a.mm2 != b.mm2) return a.mm2 < b.mm2;
}
int main()
{
	int n;
	char line[15];//暂存on/off
	for (int i = 0; i < 24; i++)
	{
		scanf("%d", &toll[i]);
	}
	scanf("%d", &n);
	for (int i = 0; i < n; i++)
	{
		scanf("%s", cust[i].id);
		scanf("%d:%d:%d:%d", &cust[i].mm1, &cust[i].dd, &cust[i].hh, &cust[i].mm2);
		scanf("%s", line);
		if (strcmp(line, "on-line") == 0) cust[i].s = 1;
		else cust[i].s = 0;
	}
	sort(cust, cust + n, cmp);
	int on = 0;
	while (on < n)
	{
		int total = 0;
		int i = 0;//定义i是为了输出的时候只有第一次会输出id和月份
		while (on < n -1 && strcmp(cust[on].id, cust[on + 1].id) == 0)
		{
			
			if (cust[on].s == 1 && cust[on + 1].s == 0)
			{
				int money = 0, time = 0;
				if(i==0) printf("%s %02d\n", cust[on].id, cust[on].mm1);//只输出一次
				i++;
				printf("%02d:%02d:%02d ", cust[on].dd, cust[on].hh, cust[on].mm2);
				printf("%02d:%02d:%02d ", cust[on + 1].dd, cust[on + 1].hh, cust[on + 1].mm2);
				cal(on, on + 1, time, money);
				total += money;
				printf("%d $%.2f\n", time, money/100.0);
			}
			on++;
		}
		if (strcmp(cust[on].id, cust[on + 1].id) != 0)
		{
			on++;
			if (total != 0) printf("Total amount: $%.2f\n", total / 100.0);
		}
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值