算法06——patA1016电话账单问题(配对排序求和)

题目描述:
  给出24h中每个小时区间的资费(cents/minute),并给出N个通话记录点,每个通话记录点都记录了姓名、当前的时刻(月:日:时:分)以及其属于通话开始(on-line)或是通话结束(off-line)。现在需要对每个人的有效通话记录进行资费计算,有效通话记录是指同一个用户能够配对的所有on-line和off-line,且这样配对需要满足:在按时间顺序排列后,两条配对的on-line和off-line对应的时间内不允许出现其他的on-line和off-line的记录。

输出要求:
  按名字的字典序从小到大的顺序输出存在有效通话记录的用户。对单个用户来说,需要输出他的姓名、账单月份(题目保证单个用户的所有记录都在同一个月产生)及有效通话记录的时长和花费,最后输出他的总资费。注意:资费的输出需要进行单位换算,把cent换算为dollar,所以结果要除以100。

输入样例:

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

输出样例:

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

样例解释(以CYLL为例):
  能找到两对配对的记录。即01:06:01(on-line)与01:08:03(off-line)、28:15:41(on-line)与28:16:05(off-line),时长分别为122min,24min。花费分别为(20x59+20x60+20x3)/100.0=24.40、(15x19+20x5)/100.0=3.85,总花费为28.25。

思路:
(1)步骤一:先给结构体Record存放单条记录的用户名、月、日、时、分以及通话的状态,然后根据题目要求对于所有记录进行排序:

  • 如果用户名不同,按照用户名字典序从小到大排列
  • 否则,如果月份不同,按照月份从小到大排序
  • 否则,如果日期不同,按照日期从小到大排序
  • 否则,如果小时不同,按照小时从小到大排列
  • 否则,如果分钟不同,按照分钟从小到大排列

因此,可以写出比较函数:

bool cmp(Record a , Record b){
    int s = strcmp(a.name , b.name);
    if(s!=0)
        return s < 0;//优先按姓名字典序从小到大排列
    else if(a.month != b.month)
        return a.month < b.month;
    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;
}

(2)步骤二:由于所有的记录均按照上述函数进行排序,因此同一个用户的记录在数组中是连续且按照时间顺序存储的。可以设置一个int型变量needPrint表示该用户是否存在有效,初始值为0。遍历该用户所有的记录,如果在needPrint为0的情况下遇到on-line则将needPrint置为1,如果在为1的情况下遇到off-line则置为2。这样遍历结束时,如果needPrint为2就表示该用户存在有效的通话记录。为了处理多个用户的数据,可以设置int型变量next记录下一个用户的首记录在数组中的下标

(3)步骤三:输出有效通话记录,由题中已知,必须保证on-line和off-line记录先后出现,数组下表相差1时才能够配对

(4)时长计算:对已知起始时间和终止时间,只要不断让起始时间加1,判断是否到达终止时间即可

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
const int maxn = 1010;
int toll[25]; //不同时间区间的资费
struct Record{ //通话记录
    char name[25];
    int month,dd,hh,mm;
    bool status;//true表示on-line,反之为off-line
}rec[maxn],temp;

bool cmp(Record a , Record b){
    int s = strcmp(a.name , b.name);
    if(s!=0)
        return s < 0;//优先按姓名字典序从小到大排列
    else if(a.month != b.month)
        return a.month < b.month;
    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 get_ans(int on ,int off , int &time , int &money){
    temp = rec[on];//该次记录的起始时间
    while(temp.dd < rec[off].dd || temp.hh < rec[off].hh || temp.mm < rec[off].mm){
        time++;//该次记录的总时间加1
        money += toll[temp.hh];//话费增加
        temp.mm++;
        if(temp.mm >= 60){
            temp.hh++;
            temp.mm = 0;
        }
        if(temp.hh >= 24){
            temp.dd++;
            temp.hh = 0;
        }
    }
}

int main(){
    for(int i = 0 ; i < 24 ;i++){
        scanf("%d",&toll[i]);//资费
    }
    int n;
    scanf("%d",&n);
    char line[10];//临时存放on-line或者off-line
    for(int i = 0 ; i < n ; i++){
        scanf("%s",rec[i].name);
        scanf("%d:%d:%d:%d",&rec[i].month,&rec[i].dd,&rec[i].hh,&rec[i].mm);
        scanf("%s",&line);
        if(strcmp(line,"on-line")==0){
            rec[i].status = true;
        }else{
            rec[i].status = false;
        }
    }
    sort(rec,rec+n,cmp);
    int on = 0 , off , next;//on和off为配对的两条记录,next为下一个用户
    while(on < n ){
        int needPrint = 0 ;
        next = on; //从当前位置寻找下一个用户
        while(next < n && strcmp(rec[next].name,rec[on].name)==0){
            if(needPrint == 0 && rec[next].status == true){
                needPrint = 1;
            }else if(needPrint ==1 && rec[next].status == false){
                needPrint = 2;
            }
            next++;
        }
        if(needPrint < 2){//没有找到一对配对的
            on = next;
            continue;
        }
        int Allmoney = 0;//总共花费的钱
        printf("%s %02d\n",rec[on].name,rec[on].month);
        while(on < next){
            while(on < next &&!(rec[on].status == true && rec[on+1].status ==false)){
                on++;
            }
            off = on +1;
            if(off == next){
                on = next;
                break;
            }
            printf("%02d:%02d:%02d ",rec[on].dd,rec[on].hh,rec[on].mm);
            printf("%02d:%02d:%02d ",rec[off].dd,rec[off].hh,rec[off].mm);
            int time = 0 ,money = 0;
            get_ans(on,off,time,money);
            Allmoney += money;
            printf("%d $%.2f\n",time , money/100.0);
            on = off+1;//完成一个配对,开始找下一个配对
        }
        printf("Total amount: $%.2f\n",Allmoney/100.0);
    }
    return 0;
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值