1016. Phone Bills (25) 字符串处理,时间模拟, 特殊情况判断

/*************************
题意:
给出一个堆通话记录
on后面接上off,才算是一次完整的通话
on后面接on,off后面接off的记录都不算。
求报告单
该报告单包含名字,以及各条通话记录,以及总通话费用。
/************************
求解要点和注意点:
先将所有记录排序,自定排序规则,先名字,再日、小时、分
然后根据名字,分离出一次单人话费记录,进行输出,并注意
1.on后面接off,才算一次真正记录生成
2.没有生成的话,这个用户的记录就不会存在!即不存在ToTal=0的情况。


计算话费我用的是累加法
即从当前on的时间一直增加1,增加至off的时间
这样能减少很多不必要的特殊处理,快速方便。


上面的第二点注意加个flag就行,没必要再循环输出一次。
************************/


/***********************
笔记:
特殊情况首先想好: 不存在在呢么办?
其次,尽量用快速方法做,能用flag就flag来判断。
*********************/


#include<iostream>
#include<stdio.h>
#include<string>
#include<vector>
#include<queue>
#include<stdlib.h>
#include<algorithm>
using namespace std;
#define M 1005
int cost[24];
struct Recoder{
string name;
int month,d,h,m;
int f;
};


struct Recoder reco[M]; 


bool cmp(struct Recoder &a,struct Recoder &b){
if(a.name<b.name)
return true;
else if(a.name==b.name){
if(a.d<b.d)
return true;
else if(a.d==b.d){
if(a.h<b.h)
return true;
else if(a.h==b.h)
return a.m<b.m;
else return false;
}
else
return false;
}else 
return false;
}


int c[24][24];
int main()
{
int i,j,k;

for(i=0;i<24;i++){
scanf("%d",&cost[i]);
}
int n;
cin>>n;
string name,fline;
int mon,d,h,m;
for(i=0;i<n;i++){
cin>>name;
scanf("%d:%d:%d:%d",&mon,&d,&h,&m);
cin>>fline;
reco[i].month=mon;
reco[i].name=name;
reco[i].d=d;
reco[i].h=h;
reco[i].m=m;
if(fline[1]=='n')
reco[i].f=0;
else reco[i].f=1;
}
sort(reco,reco+n,cmp);




int needf;
string name1,name2;
name1="";
int d0,h0,m0,d1,h1,m1;
int ftime;
int sum;
int Total;
int flag;
for(i = 0;i < n; i++){
name2 = reco[i].name;
if(name1 != name2){
if(name1!="" && flag==1)
printf("Total amount: $%.2lf\n",double(Total)/100);


name1 = name2;
Total = 0;
flag=0;
d0=reco[i].d;h0=reco[i].h;m0=reco[i].m;
if(reco[i].f==0)
needf=1;
else 
needf=0;
}
else{
if(reco[i].f==1 && needf==1){
needf=0;
d1=reco[i].d;h1=reco[i].h;m1=reco[i].m;
if(flag==0){
flag=1;
cout<<reco[i].name<<" ";
printf("%02d\n",reco[i].month);
}
printf("%02d:%02d:%02d ",d0,h0,m0);
printf("%02d:%02d:%02d ",d1,h1,m1);
sum=0;
ftime=0;
while(!(d0==d1&&m0==m1&&h1==h0)){
ftime++;
sum += cost[h0];
m0++;
if(m0==60){
m0=0;
h0++;
if(h0==24)
{h0=0;d0++;}
}
}
printf("%d ",ftime);
printf("$%.2lf\n",double(sum)/100);
Total += sum;
}
else if(reco[i].f==0){
needf=1;
d0=reco[i].d;h0=reco[i].h;m0=reco[i].m;
}
}


}
if(flag==1)
printf("Total amount: $%.2lf\n",double(Total)/100);
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值