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