1016. Phone Bills (25)

#include<iostream>
#include<cstring>
#include<algorithm>
#include<iomanip>
#define daytime 24
using namespace std;
struct PhoneBills {
	char id[21];
	bool on;
	int month;
	int dd, hh, mm;
};
void reaDln(PhoneBills * PB, int N)//这个真的厉害,oNoff读:号,时间分别读为数字
{
	char oNoff[9];
	for(int i=0;i<N;i++)
	{
		cin >> PB[i].id >> PB[i].month >> oNoff[0] >> PB[i].dd >> oNoff[0] >> PB[i].hh >> oNoff[0] >> PB[i].mm >> oNoff;
		PB[i].on = strcmp(oNoff, "on-line") == 0 ? true : false;
	}
}
bool PBcmp(PhoneBills a, PhoneBills b)//排序首先是字母序,然后是时间序,都是升序
{
	if (strcmp(a.id, b.id) != 0)
		return strcmp(a.id, b.id) < 0;
	else if (a.dd != b.dd)
		return a.dd < b.dd;
	else if (a.hh != b.hh)
		return a.hh < b.hh;
	else
		return a.mm < b.mm;
}
double zeroToNow(int h, int m, int cost[])//求一个时间从零点开始时的花费
{
	double partsum = 0;
	int i;
	for (i = 0; i < h; i++)
		partsum += 60 * cost[i];
	partsum += m*cost[i];
	return partsum;
}
int monney(int dd, int hh, int mm, int d, int h, int m, int cost[], double * temp)
{
	int totalminute = 0;
	totalminute = (d - dd);
	(*temp) = cost[24] * 60 * totalminute;	//整天数的花费
	totalminute *= (24 * 60);
	totalminute += (h - hh) * 60 + m - mm;
	(*temp) += zeroToNow(h, m, cost) - zeroToNow(hh, mm, cost);//两个时间都求一下对于零点的花费,再相减
	return totalminute;
}

int main()
{
	int cost[daytime + 1];
	double temp, sum;
	int index, N, before, tmp, totalminute;
	string idnow;
	PhoneBills * PB;
	
	for (index = 0,cost[daytime]=0; index < daytime; index++)//daytime记录了各个小时花费的和
	{
		cin >> cost[index];
		cost[daytime] += cost[index];
	}
	cin >> N;
	PB = new PhoneBills[N];
	reaDln(PB, N);
	sort(PB, PB + N, PBcmp);
	index = 1;
	temp = 0;
	sum = 0;
	totalminute = 0;
	idnow = PB[0].id;//从第一个人开始
	for (index = 1; index < N; index++)
	{
		sum = 0;
		temp = 0;
		totalminute = 0;
		for (; PB[index].id == idnow&&index < N; index++)//判断是不是同一个人的online和offline,是一个人的就进循环
		{
			before = index - 1;
			if (PB[before].on == true && PB[index].on == false)//前一个为online后一个为offline则说明找到了一个
			{
				if (sum == 0)//sum为0来标志输出下面这句话,不为零的时候输出别的通话
					cout << PB[index].id << " " << setw(2) << setfill('0') << PB[before].month << endl;
				totalminute = monney(PB[before].dd, PB[before].hh, PB[before].mm, PB[index].dd, PB[index].hh, PB[index].mm,cost,&temp);
				cout << setw(2) << setfill('0') << PB[before].dd<<":"
					<< setw(2) << setfill('0') << PB[before].hh << ":"
					<< setw(2) << setfill('0') << PB[before].mm
					<< " "
					<< setw(2) << setfill('0') << PB[index].dd << ":"//setw设置数字宽度,setfill设置空的时候补0
					<< setw(2) << setfill('0') << PB[index].hh << ":"
					<< setw(2) << setfill('0') << PB[index].mm
					<< " "
					<< totalminute <<" $" << setiosflags(ios::fixed) << setprecision(2)<< temp/100 << endl;
				sum += temp;
			}
		}
		if (sum != 0)cout << "Total amount: $" << setiosflags(ios::fixed) << setprecision(2) << sum / 100 << endl;
		if(index<N)//这个时候index刷到和前一个不同名字的地方,就让当前的idnow赋值成此时的id
		idnow = PB[index].id;
	}
	delete PB;
	return 0;
}

http://blog.csdn.net/u014646950/article/details/47102825大神博客传送

很担心三月份烤成什么鬼,,太差劲了,,

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值