1016 Phone Bills (25分)

1016 Phone Bills (25分)
花了一个小时写完代码,但由于计算花费的时候思维混乱,WA后改了半个小时才过

#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct node {
	string id;
	int mouth, time, flag, timeout;
};
bool cmp(node a, node b) {
	return a.id != b.id ? a.id < b.id : a.time < b.time;
}
int toll[24], d_cost = 0;
int cul(int a, int b) {
	int sum = 0;
	for (int i = (a / 60) + 1; i < b / 60; i++) {//相隔小时数
		sum += (toll[i] * 60);
	}
	if (a / 60 != b / 60) {//是否同一小时
		sum += (60 - (a % 60)) * toll[a / 60];
		sum += (b % 60) * toll[b / 60];
	}
	else {
		sum += (b - a) * toll[a / 60];
	}
	return sum;
}
int trans(int a, int b) {
	int sum = ((b - a) / 1440) * d_cost;//相隔天数
	a = a % 1440, b = b % 1440;
	if (a > b) {
		swap(a, b);
		sum += d_cost;
		sum -= cul(a, b);
	}
	else {
		sum += cul(a, b);
	}
	return sum;
}
int main() {
	for (int i = 0; i < 24; i++) {
		scanf("%d", &toll[i]);
		d_cost += toll[i] * 60;//一天整的花费
	}
	int n, mouth, d, h, m, time;
	scanf("%d", &n);
	vector<node> info;
	string s, flag;
	for (int i = 0; i < n; i++) {
		cin >> s;
		scanf("%d:%d:%d:%d", &mouth, &d, &h, &m);
		cin >> flag;
		int t = flag == "on-line" ? 1 : -1;
		time = d * 24 * 60 + h * 60 + m;
		node temp = { s,mouth,time,t };
		info.push_back(temp);
	}
	sort(info.begin(), info.end(), cmp);
	vector<node> costom;
	for (int i = 0; i < n - 1; i++) {
		if (info[i].id == info[i + 1].id && info[i].flag == 1 && info[i + 1].flag == -1) {
			info[i].timeout = info[i + 1].time;
			costom.push_back(info[i]);
		}
	}
	for (int i = 0, j = 0; i < costom.size(); i = j) {
		printf("%s %02d\n", costom[i].id.c_str(), costom[i].mouth);
		int sum = 0;
		for (j = i; j < costom.size() && costom[j].id == costom[i].id; j++) {
			int in = costom[j].time, out = costom[j].timeout;
			int t = trans(in, out);
			sum += t;
			printf("%02d:%02d:%02d ", in / 1440, (in % 1440) / 60, (in % 60));
			printf("%02d:%02d:%02d %d", out / 1440, (out % 1440) / 60, (out % 60), out - in);
			printf(" $%.2f\n", t / 100.0);
		}
		printf("Total amount: $%.2f\n", sum / 100.0);
	}
	return 0;
}

参考柳神,把计算过程简化,分别求出00:00:00到out_time和in_time花的钱,再相减求差
代码如下:

#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct node {
	string id;
	int mouth, time, flag, timeout;
};
bool cmp(node a, node b) {
	return a.id != b.id ? a.id < b.id : a.time < b.time;
}
int toll[24], d_cost = 0;
int culculate(int a) {//00:00:00到a花的钱
	int sum = (a / 1440) * d_cost;
	a = a % 1440;
	for (int i = 0; i < a / 60; i++) {
		sum += toll[i] * 60;//总是忘乘60
	}
	sum += (a % 60) * toll[(a / 60)];
	return sum;
}
int main() {
	for (int i = 0; i < 24; i++) {
		scanf("%d", &toll[i]);
		d_cost += toll[i] * 60;
	}
	int n, mouth, d, h, m, time;
	scanf("%d", &n);
	vector<node> info;
	string s, flag;
	for (int i = 0; i < n; i++) {
		cin >> s;
		scanf("%d:%d:%d:%d", &mouth, &d, &h, &m);
		cin >> flag;
		int t = flag == "on-line" ? 1 : -1;
		time = d * 24 * 60 + h * 60 + m;
		node temp = { s,mouth,time,t };
		info.push_back(temp);
	}
	sort(info.begin(), info.end(), cmp);
	vector<node> costom;
	for (int i = 0; i < n - 1; i++) {
		if (info[i].id == info[i + 1].id && info[i].flag == 1 && info[i + 1].flag == -1) {
			info[i].timeout = info[i + 1].time;
			costom.push_back(info[i]);
		}
	}
	for (int i = 0, j = 0; i < costom.size(); i = j) {
		printf("%s %02d\n", costom[i].id.c_str(), costom[i].mouth);
		int sum = 0;
		for (j = i; j < costom.size() && costom[j].id == costom[i].id; j++) {
			int in = costom[j].time, out = costom[j].timeout;
			int t = culculate(out) - culculate(in);//求差值
			sum += t;
			printf("%02d:%02d:%02d ", in / 1440, (in % 1440) / 60, (in % 60));
			printf("%02d:%02d:%02d %d", out / 1440, (out % 1440) / 60, (out % 60), out - in);
			printf(" $%.2f\n", t / 100.0);
		}
		printf("Total amount: $%.2f\n", sum / 100.0);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值