PATA-1016Phone Bills (25)

34 篇文章 0 订阅
13 篇文章 0 订阅

代码来自http://blog.csdn.net/cstopcoder/article/details/25559285?utm_source=tuicool&utm_medium=referral

题意:

对于给定的电话单子进行处理,注意:on必须后面有一个off匹配。而且如果这个单子不合理就不输出。PAT题里根本没叙述还有这测试点,坑死人。

思路:

自己想用map试一下,但是不太会it->second的排序,恰好网上有个写的很好的。

#include<cstdio>
#include<iostream>
#include<string>
#include<map>
#include<vector>
#include<algorithm>

using namespace std;

typedef struct Calls
{
	int month;
	int day;
	int hour;
	int minute;
	string word;
}Calls;

bool cmp(const Calls &l,const Calls &r)
{
	if(l.day<r.day)
		return true;
	else if(l.day==r.day&&l.hour<r.hour)
		return true;
	else if(l.day==r.day&&l.hour==r.hour&&l.minute<r.minute)
		return true;
	else
		return false;
}

map<string,vector<Calls> > m;//存储名字对应的通话记录
int Rate[24];

float ComputeFee(Calls l,Calls r,int *totaltime)//计算通话费用
{
	float fee=0;
	*totaltime=0;
	while(l.day<r.day||l.hour<r.hour||l.minute<r.minute)
	{
		fee+=Rate[l.hour];
		(*totaltime)++;
		l.minute++;
		if(l.minute==60)
		{
			l.minute=0;
			l.hour++;
			if(l.hour==24)
			{
				l.hour=0;
				l.day++;
			}
		}
	}
	return fee;
}

int main(int argc,char *argv[])
{
	int i,j;
	int n;
	for(i=0;i<24;i++)
		cin>>Rate[i];
	cin>>n;
	for(i=0;i<n;i++)
	{
		string name;
		Calls temp;
		cin>>name;
		scanf("%d:%d:%d:%d",&temp.month,&temp.day,&temp.hour,&temp.minute);
		cin>>temp.word;
		m[name].push_back(temp);
	}
	map<string,vector<Calls> >::iterator it;
	vector<Calls>::iterator p;
	for(it=m.begin();it!=m.end();it++)
	{
		sort((it->second).begin(),(it->second).end(),cmp);
	}   
	for(it=m.begin();it!=m.end();it++)
	{   
		int FindPair=false;
		int totaltime=0;
		float total=0;
		for(p=(it->second).begin();p!=(it->second).end();p++)
		{                   
			Calls temp=*p;
			if(temp.word[1]=='n')
			{
				if((p+1)!=(it->second).end())
				{
					Calls next=*(p+1);
					if(next.word[1]=='f')
					{
						if(!FindPair)
						{
							cout<<it->first;
							printf(" %02d\n",p->month);
							FindPair=true;
						}
		            printf("%02d:%02d:%02d %02d:%02d:%02d",temp.day,
                        temp.hour,temp.minute,next.day,next.hour,next.minute);
                    float fee=ComputeFee(temp,next,&totaltime);
					printf(" %d $%.2f\n",totaltime,fee/100);
					total+=fee;
					}
				}
			}
		}
		if(FindPair)
	    	printf("Total amount: $%.2f\n",total/100);
	}   

	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值