1016. Phone Bills (25) @ PAT (Advanced Level) Practise

20 篇文章 0 订阅

自己写的只得了19分,还有6分的case没有通过,代码写的略渣。

附个人家的链接:http://blog.csdn.net/sunbaigui/article/details/8657062

#include<iostream>
#include<stdio.h>
#include<list>
#include<vector>
#include<string>
#include<algorithm>
#include<sstream>
using namespace std;

int cpm[24];    //cents per minute
int timeCount[24];
int N;
struct record
{
	string name;
	string time;
	string line;
	//bool line;    //true - 'on-line';false - 'off-line'
	bool operator < (const record & tmp) const
	{
		if(name!=tmp.name)
			return name<tmp.name;
		else
			return time<tmp.time;
	}
};

list<record> rl;
list<record>::iterator rlit;
vector<string> split(string str,string d)
{
	vector<string> result;
	int bp=0;
	int pos=0;
	int length=str.length();

	while(bp<length && pos!=-1)
	{
		pos=str.find(d,bp);
		if(pos!=bp)
			result.push_back(str.substr(bp,pos-bp));
		bp=pos+d.length();
	}
	return result;
}
int main()
{
	struct record tr;
	string cName;//current Name
	int time;//time of each conversation
	string month;

	for(int i=0;i<24;i++)
	{
		cin>>cpm[i];
	}
	
	cin>>N;
	//cin record list
	int p=0;
	while(N--)
	{
		cin>>tr.name>>tr.time>>tr.line;
		p=tr.time.find(":");
		if(N==0)
		{
			month=tr.time.substr(0,p);
		}
		tr.time=tr.time.substr(p+1);
		rl.push_back(tr);
	}
	//sort record list
	rl.sort();
	bool flag=false;
	struct record ra,rb;
	int d1,d2,h1,h2,m1,m2;
	int hourCount=0;
	int hp=0;
	float sum=0;
	float cost=0;
	while(rl.size()>=2)
	{
		//last on-line match first off-line
		while(rl.front().line=="off-line")
			rl.pop_front();
		//find ra;
		ra=rl.front();
		rl.pop_front();

		//find rb;
		if(rl.front().name==ra.name && rl.front().line=="off-line")
		{
			rb=rl.front();
			rl.pop_front();
		}
		else
			continue;
		//do work
		stringstream ss;
		vector<string> sl=split(ra.time,":");
		ss<<sl[0];ss>>d1;ss.clear();
		ss<<sl[1];ss>>h1;ss.clear();
		ss<<sl[2];ss>>m1;ss.clear();
		sl=split(rb.time,":");
		ss<<sl[0];ss>>d2;ss.clear();
		ss<<sl[1];ss>>h2;ss.clear();
		ss<<sl[2];ss>>m2;ss.clear();
		hourCount=(d2-d1)*24+h2-h1;
		for(int i=0;i<24;i++)
			timeCount[i]=0;
		hp=h1;
		timeCount[hp]=60-m1;
		for(int i=1;i<hourCount;i++)
		{
			++hp;
			if(hp>23)
				hp-=24;
			timeCount[hp]+=60;
		}
		timeCount[hp+1]+=m2;

		cost=0;
		for(int i=0;i<24;i++)
			cost+=timeCount[i]*cpm[i];
		if(cName!=ra.name)
		{	
			cName=ra.name;	
			if(flag)
			{
				printf("Total amount: $%.2f\n",sum/100);
			}
			sum=cost;
			printf("%s %s\n%s %s %d $%.2f\n",cName.c_str(),month.c_str(),ra.time.c_str(),rb.time.c_str(),(((d2-d1)*24+h2-h1)*60+m2-m1),cost/100);
			flag=true;
		}
		else
		{
			sum+=cost;
			printf("%s %s %d $%.2f\n",ra.time.c_str(),rb.time.c_str(),(((d2-d1)*24+h2-h1)*60+m2-m1),cost/100);
		}

	}
	printf("Total amount: $%.2f\n",sum/100);
	rl.clear();

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值