1016 Phone Bills (25分)

这道题主要是排完序之后的处理思路。首先先找出一个用户的所有的通话记录(边界);第二步边界里看这个用户有没有有效的通话记录,若有则把该用户所有的有效记录输出,若没有就找下一个用户。依此处理完所有的通话记录。

#include<cstdio>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
struct record{
	string name;
	int MM,dd,HH,mm;
	bool state;
};
bool cmp(record a, record b)
{
	if(a.name!=b.name) return a.name<b.name;
	else if(a.MM!=b.MM) return a.MM<b.MM;
	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;
}
void fun(record a, record b, int* time,double* price,int toll[])
{
	*time = 0; *price = 0;
	while(a.dd<b.dd||a.HH<b.HH||a.mm<b.mm){
		*price+=toll[a.HH];
		a.mm++;
		if(a.mm==60){
			a.mm = 0;
			a.HH++;
		}
		if(a.HH==24){
			a.HH = 0;
			a.dd++;
		}
		(*time)++;
	}
}
int main()
{
	int n;
	int toll[24];
	string state;
	for(int i=0; i<24; i++){
		scanf("%d",&toll[i]);
	}
	scanf("%d",&n);
	record re[n];
	for(int i=0; i<n; i++){
		cin>>re[i].name;
		scanf("%d:%d:%d:%d",&re[i].MM,
		&re[i].dd,&re[i].HH,&re[i].mm);
		cin>>state;
		if(state=="on-line") re[i].state=true;
		else re[i].state=false;
	}
	sort(re,re+n,cmp);
	int i=0;
	while(i<n){
		int low, high;
		low = i; high = i+1;
		while(high<n&&re[high].name==re[low].name){
			high++;
		}
		bool needPrint = false;
		int j = low;
		while(j<high-1){
			if(re[j].state&&!re[j+1].state){
				needPrint = true;
				break;
			}
			j++;
		}
		if(needPrint){
			cout<<re[low].name;
			printf(" %02d\n",re[low].MM);
			int k = low;
			double total = 0.0;
			while(k<high-1){
				if(re[k].state&&!re[k+1].state){
					int time; double price;
					fun(re[k],re[k+1],&time,
					&price,toll);
					printf("%02d:%02d:%02d ",
					re[k].dd,re[k].HH,re[k].mm);
					printf("%02d:%02d:%02d ",
					re[k+1].dd,re[k+1].HH,re[k+1].mm);
					printf("%d $%.2f\n",time,price/100);
					total+=price;
					k+=2;
				}
				else k++;
			}
			printf("Total amount: $%.2f\n",total/100);
		}
		i = high;
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值