1016. Phone Bills

题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1016


// 时间问题
// 选取基准,统一到一个标准之上做减法


#include <stdio.h>
#include <string.h>

#include <iostream>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>

#define SIZE 1000+10

using namespace std;

struct Call
{
	string name;
	int month, day, hh, mm, total;
	string mark;
};


vector <Call> call_list;
vector <Call> format_list;
int N, charge[24];


// function
int chargeByTime(Call x);
int Time(Call s, Call e);
double Cost(Call s, Call e);
void Input();
bool cmp(Call a, Call b);
void show(vector<Call> v);
void formFormat();

int chargeByTime(Call x)
{
	int hour = x.total/60;
	int minutes = x.total%60;
	int money=0;
	int i;
	for(i=0; i<hour; i++)
	{
		money += charge[i%24]*60;
	}
	money += charge[i%24]*minutes;
	return money;
}


int Time(Call s, Call e)
{
	return e.total-s.total;
}

double Cost(Call s, Call e)
{
	return (chargeByTime(e)-chargeByTime(s))/100.0;
}

void Input()
{
	int i;
	for(i=0; i<24; i++)
	{
		cin >> charge[i];
	}
	cin >> N;
	Call c;
	while(N-->0)
	{
		getchar();
		cin >> c.name;

		cin >> c.month;
		getchar();
		cin >> c.day;
		getchar();
		cin >> c.hh;
		getchar();
		cin >> c.mm;
		getchar();
		cin >> c.mark;

		c.total = c.day*24*60 + c.hh*60 + c.mm;
		call_list.push_back(c);
	}
}

bool cmp(Call a, Call b)
{
	if(a.name != b.name)
		return a.name<b.name;
	else 
	{	
		if(a.total != b.total)
		return a.total < b.total;
	}
}

void formFormat()
{
	string curname="";
	bool isonline=false;
	vector<Call>::iterator it;
	for(it = call_list.begin(); it  != call_list.end(); it++)
	{
		if(isonline== false && (*it).mark =="on-line" )
		{
			isonline=true;
			format_list.push_back(*it);
			curname = (*it).name;
		}
		else if(isonline == true && (*it).mark  == "on-line")
		{
			isonline=true;
			format_list.pop_back();
			format_list.push_back(*it);
			curname = (*it).name;
		}
		else if(isonline == true && (*it).mark == "off-line" && curname == (*it).name)
		{
			isonline = false;
			format_list.push_back(*it);	
		}
	}

	// the last must be off-line
	if( (*(format_list.end()-1)).mark == "on-line")
	{
		format_list.pop_back();
	}
}

void show(vector<Call> v)
{
	int i;
	for(i=0; i<v.size(); i++)
	{
		cout << v[i].name << " ";
		cout << v[i].month << ":";
		cout << v[i].day << ":";
		cout << v[i].hh << ":";
		cout << v[i].mm << " ";
		cout << v[i].mark << endl;
	}
}

void Output()
{
	int num=0;
	double sum=0;
	int i;
	string curname="";
	for(i=0; i<format_list.size(); i+=2)
	{
		num++;
		if(curname != format_list[i].name)
		{
			if(num != 1)
			{
				printf("Total amount: $%.2lf\n", sum);
				sum = 0;
			}
			curname=format_list[i].name;
			printf("%s %02d\n", curname.c_str(), format_list[i].month);
		}

		printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2lf\n", format_list[i].day, format_list[i].hh, format_list[i].mm,
			format_list[i+1].day, format_list[i+1].hh, format_list[i+1].mm, Time(format_list[i], format_list[i+1]),
			Cost(format_list[i], format_list[i+1]));
		sum += Cost(format_list[i], format_list[i+1]);
	}
	printf("Total amount: $%.2lf\n", sum);// the last one
}

int main()
{
	#ifdef ONLINE_JUDGE
	#else
		freopen("E:\\in.txt", "r", stdin);
	//	freopen("E:\\out.txt", "w", stdout);
	#endif

	Input();
	sort(call_list.begin(), call_list.end(), cmp);
	formFormat();
	Output();
	return 0;
}





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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值