很考察耐心的一道题,写起来也不是很复杂,耐心去做肯定是能写出来的啦
下面介绍一下auto的用法,比如,int或者double,我们可以写成,int i=0;double k=0.0,那么auto相当于我没有指代这个究竟是什么类型的,我可以写成,auto it=s.begin()什么的,这样我们在使用map之类的就很方便啦,至于map的用法,可以参考晴神算法笔记第六章的内容
题目大意是让你计算电话费,并且要求是临近配对
所以我们可以先排序,名字不同按照名字大小排,名字相同按照时间(这里因为都是在同一月份,所以可以把总时间计算出来,这样时间小的就在前面,大的就在后面)排,那么我们可以想到,现在是一组非常有序的数字,临近如果满足前一个和后一个名字相同且状态一个是开,一个是关,那么就说明配对成功了,成功之后我们可以把它存在map数组里,这样同一个人,每一次通话都能记录在这个数组里,最后,我们通过遍历(遍历两次,一次是不同人,一次是同一人的不同时间)
这里用到的小技巧:时间可以直接都换算成分钟,那么两次相减就是通话时间了,同样的,计算账单分别计算那么最后相减就是本次账单了。这个题看起来要写很长,但是利用map就会比较简单了,这次比较啰嗦,因为一开始也觉得自己写不出来,而且这个很长,但是还是写出来了。
坑点:数组之间不能直接比较,如果直接比较两个人的名字会没有结果的,所以要用string来定义这个数组,也可以用strcmp吧。代码参考了柳神的代码,基本上没有什么大的改动。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<map>
#include<vector>
using namespace std;
const int maxn=1000;
struct node
{
string name;
int time, month,dd,hh,mm;
int status;
} a[maxn];
int rate[25]= {0};
double bill(node call)
{
double total=rate[call.hh]*call.mm+rate[24]*60*call.dd;
for (int i=0;i<call.hh;i++)
total+=rate[i]*60;
return total/100;
}
bool cmp(node b,node c)
{
return b.name!=c.name?b.name<c.name:b.time<c.time;
}
int main()
{
int n;
for (int i=0; i<24; i++)
{
scanf("%d",&rate[i]);
rate[24] += rate[i];
}
scanf("%d",&n);
for (int i=0; i<n; i++)
{
cin>>a[i].name;
scanf("%d:%d:%d:%d",&a[i].month,&a[i].dd,&a[i].hh,&a[i].mm);
string t;
cin>>t;
a[i].status=(t=="on-line")? 1 : 0;
a[i].time=a[i].dd*24*60+a[i].hh*60+a[i].mm;
}
sort(a,a+n,cmp);
map <string,vector<node>> custom;
for (int i=1; i<n; i++)
{
if (a[i].name==a[i-1].name&&a[i-1].status==1&&a[i].status==0)
{
custom[a[i-1].name].push_back(a[i-1]);
custom[a[i].name].push_back(a[i]);
}
}
for (auto it : custom)
{
vector <node> temp=it.second;
cout<<it.first;
printf(" %02d\n",temp[0].month);
double total=0;
for (int i=1;i<temp.size();i+=2)
{
double t=bill(temp[i])-bill(temp[i-1]);
int time=temp[i].time-temp[i-1].time;
printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",temp[i-1].dd,temp[i-1].hh,temp[i-1].mm,temp[i].dd,temp[i].hh,temp[i].mm,time,t);
total+=t;
}
printf("Total amount: $%.2f\n",total);
}
return 0;
}