概述
Sample Input:10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10
10
CYLL 01:01:06:01 on-line
CYLL 01:28:16:05 off-line
CYJJ 01:01:07:00 off-line
CYLL 01:01:08:03 off-line
CYJJ 01:01:05:59 on-line
aaa 01:01:01:03 on-line
aaa 01:02:00:01 on-line
CYLL 01:28:15:41 on-line
aaa 01:05:02:24 on-line
aaa 01:04:23:59 off-line
Sample Output:CYJJ 01
01:05:59 01:07:00 61 $12.10
Total amount: $12.10
CYLL 01
01:06:01 01:08:03 122 $24.40
28:15:41 28:16:05 24 $3.85
Total amount: $28.25
aaa 01
02:00:01 04:23:59 4318 $638.80
Total amount: $638.80
按用户分组打印月账单
关键点1.处理好记录间的匹配,删掉坏的记录
2.不同时段价不同,计算好价格
此题好烦。。。。。。
#include<iostream>
using namespace std;
#include<vector>
#include<string>
#include<algorithm>
struct Call
{
string name;
int month;
int date;
int hour;
int minute;
int total;
string status;
};
int charge[24];
vector<Call> all_calls;
vector<Call> format_calls;
bool compare(Call a,Call b)
{
if(a.name < b.name)
return 1;
else if(a.name == b.name && a.total < b.total)
return 1;
else
return 0;
}
void show(Call c)
{
cout<<c.name<<" ";
cout<<c.month<<":"<<c.date<<":"<<c.hour<<":"<<c.minute<<" ";
cout<<c.status<<endl;
}
//calculate money from the begin of month to the time given
int chargeByTime(int time)
{
int hours = time/60;
int minutes = time%60;
int money = 0;
int i;
for(i = 0;i<hours;i++)
money += charge[i%24]*60;
money += charge[i%24]*minutes;
return money;
}
double calCost(Call s,Call t)
{
return (double)(chargeByTime(t.total)-chargeByTime(s.total))/100;
}
int calLast(Call s,Call t)
{
return (t.date-s.date)*24*60+(t.hour-s.hour)*60+(t.minute-s.minute);
}
int main()
{
for(int i = 0;i<24;i++)
cin>>charge[i];
int N;
cin>>N;
while(N--)
{
Call c;
cin>>c.name;
cin>>c.month;
getchar();
cin>>c.date;
getchar();
cin>>c.hour;
getchar();
cin>>c.minute;
c.total = c.minute + 60*c.hour + 24*60*c.date;
cin>>c.status;
all_calls.push_back(c);
}
sort(all_calls.begin(),all_calls.end(),compare);
//filter delete those bad record
bool haveonline = false;
string curname;
for(int i=0;i<all_calls.size();i++)
{
if(haveonline == false && all_calls[i].status =="on-line" )
{
format_calls.push_back(all_calls[i]);
haveonline = true;
curname = all_calls[i].name;
}
else if(haveonline == true && all_calls[i].status =="on-line")
{
format_calls.pop_back();
format_calls.push_back(all_calls[i]);
haveonline = true;
curname = all_calls[i].name;
}
else if(haveonline == true && all_calls[i].status =="off-line"&&all_calls[i].name ==curname)
{
format_calls.push_back(all_calls[i]);
haveonline = false;
}
}
//the last must be offline
if((*(format_calls.end()-1)).status == "on-line")
format_calls.pop_back();
//output
double totalcost = 0;
curname = "";
for(int i=0;i<format_calls.size();i+=2)
{
if(format_calls[i].name != curname)
{
if(curname!="")
{
printf("Total amount: $%.2fn",totalcost);
totalcost = 0;
printf("%s %02dn",format_calls[i].name.c_str(),format_calls[i].month);
}
else
{
printf("%s %02dn",format_calls[i].name.c_str(),format_calls[i].month);
}
curname = format_calls[i].name;
}
printf("%02d:%02d:%02d",format_calls[i].date,format_calls[i].hour,format_calls[i].minute);
printf(" ");
printf("%02d:%02d:%02d",format_calls[i+1].date,format_calls[i+1].hour,format_calls[i+1].minute);
printf(" ");
printf("%d",calLast(format_calls[i],format_calls[i+1]));
printf(" ");
printf("$%.2fn",calCost(format_calls[i],format_calls[i+1]));
totalcost+=calCost(format_calls[i],format_calls[i+1]);
}
printf("Total amount: $%.2fn",totalcost);
}
最后
以上就是可爱眼神为你收集整理的pat-1016 Phone Bills的全部内容,希望文章能够帮你解决pat-1016 Phone Bills所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复