PAT Advanced 1016. Phone Bills (C语言实现)

我的PAT系列文章更新重心已移至Github,欢迎来看PAT题解的小伙伴请到Github Pages浏览最新内容。此处文章目前已更新至与Github Pages同步。欢迎star我的repo

题目

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. Each case has two parts: the 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 ( \le 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

思路

大体思路:

  • 题目给了不同人的不同通话记录, 要求输出每个人的账单. 那么最好我们在读取了数据之后, 立即对数据进行排序. 首先对名称排序, 其次对时间排序. 这样后面就好处理了.
  • 具体处理数据, 对比每相邻的两个记录, 以确定是否是同一个人/是否是同一组通话记录
    • 如果两组记录姓名不同, 则为一个新的账单, 此时需要输出总结, 将相关变量归零
    • 如果不是上述情况, 并且两组记录分为为'on-line'和'off-line', 那么则找到一组通话记录. 此时需计算通话费用, 累计总费用以及输出此记录信息.

数据结构:

  • struct[N + 1]: 结构体包括名称, 月, 日, 时, 分, 时间(归算为分钟)和通话状态. 数组要比数据多1是因为要用最后一个作为空白对照, 以减少特殊情况的处理

注意的点:

  • 计算费用
    • (在其他博客看到的方法)计算从0到开始/结束时间的费用, 两者相减. 我觉得这种方法是最简单的, 实现容易.
    • 我的方法是从开始到结束, 对每小时内的情况进行处理, 照顾到不足一小时的情况, 具体实现见calccharge函数.
  • 输出, 除了上面思路中谈到的何时输出什么, 还有一点应该注意
    • 对于每个人, 如果没有需要输出的记录, 那么就什么都不要输出, 所以只能在明确有合理通话记录时, 再输出账单开头的姓名和月份的信息

代码

最新代码@github,欢迎交流

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/* Structure to store phone records */
typedef struct {
    char name[21];
    int month, day, hour, min, time, state;
}Record, *pRecord;

/* Compare first by name, then by date and time */
int cmp(const void *record1, const void *record2)
{
    pRecord r1 = *(pRecord*)record1, r2 = *(pRecord*)record2;
    return strcmp(r1->name, r2->name) ?
        strcmp(r1->name, r2->name) : r1->time - r2->time;
}

/* Calculate the charge of the call with start record p1 and end record p2 */
int calccharge(pRecord p1, pRecord p2, int toll[])
{
    int charge = 0, start = p1->time, end = p2->time, h, time1, time2;

    for(time1 = start; time1 < end; time1 = time2)
    {   /* Add the charge hour by hour */
        time2 = (time1 / 60 + 1) * 60; /* time2 will be the time of next hour */
        h = time1 / 60 % 24;           /* h will be the index of the hour */
        charge += ((time2 > end ? end : time2) - time1) * toll[h];
    }

    return charge;
}

int main()
{
    char state[9];
    int N, toll[24], charge, charge_total = 0;
    Record records[1001] = {0};
    pRecord precords[1001] = {0}, *p = precords;

    /* Read data */
    for(int i = 0; i < 24; i++)
        scanf("%d", toll + i);
    scanf("%d", &N);
    for(int i = 0; i < N; i++, p++)
    {
        *p = records + i;
        scanf("%s %d:%d:%d:%d %s", (*p)->name,
              &(*p)->month, &(*p)->day, &(*p)->hour, &(*p)->min, state);
        (*p)->time = ((*p)->day * 24 + (*p)->hour) * 60 + (*p)->min;
        (*p)->state = strcmp(state, "on-line") ? 0 : 1;
    }

    /* Sort first by name, then by date and time */
    qsort(precords, N, sizeof(pRecord), cmp);

    /* Print phone bill one by one */
    for(p = precords + 1; *p; p++)
    {
        if(strcmp((*p)->name, (*(p - 1))->name))
        {            /* A new customer, print last total amount if any */
            if(charge_total)
                printf("Total amount: $%.2f\n", charge_total * 1e-2);
            charge_total = 0;
        }
        else if((*(p - 1))->state == 1 && (*p)->state == 0)
        {            /* Still the same customer, finding on/off record pair */
            if(charge_total == 0)
                printf("%s %02d\n", (*p)->name, (*p)->month);
            charge = calccharge(*(p - 1), *p, toll);
            charge_total += charge;
            /* Print info of this call */
            printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",
                   (*(p - 1))->day, (*(p - 1))->hour, (*(p - 1))->min,
                   (*p)->day, (*p)->hour, (*p)->min,
                   (*p)->time - (*(p - 1))->time, charge * 1e-2);
        }
    }
    if(charge_total)
        printf("Total amount: $%.2f\n", charge_total * 1e-2);

    return 0;
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 205,386评论 6 479
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 87,939评论 2 381
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 151,851评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 54,953评论 1 278
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 63,971评论 5 369
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,784评论 1 283
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,126评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,765评论 0 258
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,148评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,744评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,858评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,479评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,080评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,053评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,278评论 1 260
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,245评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,590评论 2 343

推荐阅读更多精彩内容