PAT 1016(排序)

1016 Phone Bills (25分)


A long-distance telephone company charges its customers by the following rules:

Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a long-distance call, the time will be recorded, and so will be the time when the customer hangs up the phone. Every calendar month, a bill is sent to the customer for each minute called (at a rate determined by the time of day). Your job is to prepare the bills for each month, given a set of phone call records.

Input Specification:

Each input file contains one test case he rate structure, and the phone call records.

The rate structure consists of a line with 24 non-negative integers denoting the toll (cents/minute) from 00:00 - 01:00, the toll from 01:00 - 02:00, and so on for each hour in the day.

The next line contains a positive number N (≤1000), followed by N lines of records. Each phone call record consists of the name of the customer (string of up to 20 characters without space), the time and date (mm:dd:hh:mm), and the word on-line or off-line.

For each test case, all dates will be within a single month. Each on-line record is paired with the chronologically next record for the same customer provided it is an off-line record. Any on-line records that are not paired with an off-line record are ignored, as are off-line records not paired with an on-line record. It is guaranteed that at least one call is well paired in the input. You may assume that no two records for the same customer have the same time. Times are recorded using a 24-hour clock.

Output Specification:

For each test case, you must print a phone bill for each customer.

Bills must be printed in alphabetical order of customers' names. For each customer, first print in a line the name of the customer and the month of the bill in the format shown by the sample. Then for each time period of a call, print in one line the beginning and ending time and date (dd:hh:mm), the lasting time (in minute) and the charge of the call. The calls must be listed in chronological order. Finally, print the total charge for the month in the format shown by the sample.

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

 


一、试题分析

一个长途电话公司按以下规则收费:依据打电话的时间,长途电话每分钟将花费一定的费用。当一个用户开始打长途电话,时间将会被记录,并且当用户挂掉电话时间也会被记录。每个月月度账单发送给用户(费率由一天中的时间决定)。你的工作是根据电话通话记录准备每个月的账单。

输入信息:

  • 费率(24个非负整数):0-1、1-2 ... 22-23、23-24
  • 记录数(N≤1000):每次测试的每条记录都在同一个月;按时间顺序同一个用户on-line记录的下一条为off-line记录,未与离线记录配对的任何在线记录都将被忽略,未与在线记录配对的离线记录也会被忽略。 确保输入中至少有一个呼叫配对良好。 您可能会假设同一客户的两个记录没有相同的时间。 使用24小时制记录时间。
  1. 用户名:20个字符不包含空格
  2. 时间日期:月:日:时:分
  3. 标识:on-line / off-line

输出信息:按照用户名字的字典顺序

  •  第一行:
  1. 用户名
  2. 账单的月份(按照输出用例的格式)
  • 接下来几行:(按照时间顺序)
  1. 每次通话的开始时间、结束时间(按照dd:hh:mm的格式
  2. 持续总时间(按分钟计算)
  3. 本次通话花费多少钱
  • 最后一行:(按照输出用例的格式)
  1. 该用户本月的总花费

二、解题思路:

我们要选出每个用户有效的通话信息(按时间顺序同一个用户on-line记录的下一条为off-line记录),所以要将所有输入记录先按照用户名字典顺序排序、再按照时间日期排序(日、时、分);排好序之后在每个用户通话记录内选出匹配的记录;计算配对时间的分钟数以及消费金额

  • 存储结构:
  1. 将记录作为一个结构体:存储记录的用户名、开始时间、结束时间、标志
  2. 24小时的区间费率:存储到数组中
  • 按规则输入:
  1. 用cin不便于输入特定格式的字符且耗时长,所以这里用scanf和printf(最好不要和cin cout混用)
  2. 字符串使用char[](string类型不好用scanf表示)
  • 排序:
  1. sort(rec,rec+n,cmp);
  2. 重写cmp函数指定排序规则
  • 选出每个用户配对的记录:
  1. 判断是否有有效通话记录:以needprint(=0)作为标志(遇到on则=1,在此基础上遇到off则=2;没有有效记录则查找下一个用户)
  2. 有有效记录则进一步查找配对的记录(此时已经确定了一个用户的范围[on,next)):

  (2.1)确定on的下标值:按一定条件找到配对的on-line和off-line记录

  (2.2)确定off的下标值:off=on+1(若off出界了则说明已无配对记录了)

  (2.3)计算本次配对记录的花费:不断将起始时间加1,判断是否到达终止时间,并每分钟花费按照费率相加(单位转换美分->美元;保留两位小数)

#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;

const int maxn=1010;
int toll[25]; //每小时区间内:每分钟多少钱
//结构体
struct Record{
    char name[25]; //姓名最多20个字符
    int month,dd,hh,mm;
    bool status; //true表示记录为online,否则为offline
}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){ //不断将起始时间加1,判断是否到达终止时间
        time++; //该次记录总时间加1min
        money+=toll[temp.hh]; //按照本小时区间的费率相加
        temp.mm++;
        if(temp.mm>=60){
            temp.mm=0;
            temp.hh++;
        }
        if(temp.hh>=24){
            temp.hh=0;
            temp.dd++;
        }
    }
}
int main()
{
    //数据的输入
    for(int i=0;i<24;i++){ //24小时每小时区间的资费
        scanf("%d",&toll[i]);
    }
    int n;
    scanf("%d",&n); //记录数
    char line[10]; //临时存放on-line和off-line的输入
    for(int i=0;i<n;i++){ //n条记录的具体信息
        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;
    int off;
    int next;
    while(on<n){
        //判断是否有配对、记录下一个用户编号next
        int needprint=0;
        next=on; //从当前位置开始寻找下一个用户
        while(next<n&&strcmp(rec[next].name,rec[on].name)==0){
            if(needprint==0&&rec[next].status==true){ //找到on
                needprint=1;
            }else if(needprint==1&&rec[next].status==false){ //找到on之后的第一个off
                needprint=2;
            }
            next++; //不断递增,直到找到下一个用户
        }
        if(needprint<2){ //该用户没有匹配的记录on-off
            on=next;
            continue; //继续找下一个用户
        }

        int allmoney=0;
        printf("%s %02d\n",rec[on].name,rec[on].month); //用户名和月份
        while(on<next){
            while(on<next-1&&!(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;
            int 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、付费专栏及课程。

余额充值