返回目录![在这里插入图片描述](https://img-blog.csdnimg.cn/20200401200958932.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ExOTIwOTkzMTY1,size_16,color_FFFFFF,t_70)
题意
输入24小时,每个小时的电话费多少,再输入n个电话记录,格式为(用户名 月份:日期:小时:分钟 on-line/off-line)
按照字典序输出存在有用收费的用户记录,输出格式为:
用户名 月份
online1 offline1 分钟数1 $话费1
online2 offline2 分钟数2 $话费2
Total amount: $总话费
注意点
- 本题大致思路为先对记录进行排序,再按照排序好的记录,找到用户的记录起点i和终点next(下一个记录的起点),再在i-next的记录里输出用户的资费信息
- 排序的cmp写法:按照用户的字典序(strcmp(a.name,b.name)),再按照时间的先后顺序比较
- 找到next的记录过程中,用needprint标记当前用户是否有on-line和off-line连在一起的记录,如果有needprint值会变成2,否则不用输出
- print()函数用来输出相邻两个有效记录的时间差和money,除了本题使用循环的方式计算外,还可以使用以分为单位的作差法,但由于每个小时的资费不同,计算起来较为麻烦
- 输出钱时要将cent转换成dollar,所以要除以100
#include <bits/stdc++.h>
using namespace std;
struct record{
char name[25];
int mm,dd,hh,ss;//月份:日期:小时:分钟
int on;//0表示离线,1表示上线
}rec[1010],a,b;
int cost[24];
bool cmp(record a,record b){
int s=strcmp(a.name,b.name);
if(s!=0)return s<0;
if(a.mm!=b.mm)return a.mm<b.mm;
if(a.dd!=b.dd)return a.dd<b.dd;
if(a.hh!=b.hh)return a.hh<b.hh;
return a.ss<b.ss;
}
void print(int on,int& money){
a=rec[on];b=rec[on+1];
int time=0;
while(a.dd<b.dd||a.hh<b.hh||a.ss<b.ss){
time++;
money+=cost[a.hh];
a.ss++;
if(a.ss>=60){
a.ss=0;
a.hh++;
}
if(a.hh>=24){
a.hh=0;
a.dd++;
}
}
printf("%d $%.2f\n",time,money/100.0);
}
int main(){
for(int i=0;i<24;i++)scanf("%d",&cost[i]);
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
char line[10];
scanf("%s %d:%d:%d:%d %s",rec[i].name,&rec[i].mm,&rec[i].dd,&rec[i].hh,&rec[i].ss,line);
if(strcmp(line,"on-line")==0)rec[i].on=1;
else rec[i].on=0;
}
sort(rec,rec+n,cmp);
int i=0,next;
while(i<n){
int needprint=0;
next=i;
while(next<n&&strcmp(rec[i].name,rec[next].name)==0){
if(needprint==0&&rec[next].on==1)needprint=1;
if(needprint==1&&rec[next].on==0)needprint=2;
next++;
}
if(needprint<2){
i=next;continue;
}
printf("%s %02d\n",rec[i].name,rec[i].mm);
int allmoney=0;
while(i<next){
while(i<next-1&&rec[i].on-rec[i+1].on!=1)i++;
if(i+1==next){
i=next;
break;
}
printf("%02d:%02d:%02d ",rec[i].dd,rec[i].hh,rec[i].ss);
printf("%02d:%02d:%02d ",rec[i+1].dd,rec[i+1].hh,rec[i+1].ss);
int money=0;
print(i,money);
allmoney+=money;
i++;
}
//Total amount: $638.80
printf("Total amount: $%.2f\n",allmoney/100.0);
}
return 0;
}