PTA A1016

A1016 Phone Bills (25 分)

Title Contents

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 (≤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

word

bills

English / bɪls / US / bɪls /

n bill;. motions (bill complex)

n (Bills) name;. (English, German) Beers

v. billing (bill third person singular)

calendar

English / 'kælɪndə / US /' kæləndɚ /

. n calendar; [days] calendar; schedule
. vt be included in the table ...; ... into the schedule

denoting

Indication (denote the present participle)

toll

English / təʊl / US / Tol /
. VT collection; bell

n tolls; The consideration; bell; Casualties

. Vi ring the bell; taxation

cent

English / sent / US / sɛnt /

. N points; pennies; Vincent (equal to one hundredth of semitones)

. N (Cent) name; (method) Sang

chronologically

English / krɔnə'lɔdʒikli / US / krɔnə'lɔdʒikli /
ADV. Chronological ground

paired

English / peəd / US / peəd /
ADJ [count] pairs;. Paired

. V make pairs; fitting (pair past participle)

alphabetical

English / ælfə'betɪk (ə) l / US /, ælfə'bɛtɪkl /
ADJ letters;. [Count] alphabetical order

Topic analysis

Computational expense bills, which are essentially the sequencing and computational cost, can be used to sort qsort, yesterday I specially brushed up, wrote a blog [C / C ++] using the method and details of qsort function .
Sort by name order first, then the sequence according to the time you sort (For comparison I put all the time converted into the number of minutes), row after a good sequence to determine the user has no valid record, if there is, depending on whether there are two in each name different records and neighboring states, if the output is to calculate the number of minutes the start time and end time to calculate the amount of the bill.
On the calculation of the amount of the bill, looks very simple, but the situation is still a lot of broken up, I started to do that, is to divide what happens within an hour, a day when a plurality of hours, but the number of hours discontent across the sky One day, the situation for several days, these cases correspond to the extreme data, regardless of a certainly can not, get a lot of trouble. Then I used a method most Buddha department, minute by minute traverse ..... Finally, I come to a conclusion, the problem can be solved simply do not Points Scrapped.

Specific code

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define ONLINE 0
    #define OFFLINE 1

    int fare[24];

    struct record
    {
        char user[21];
        int min;
        int line_char;
    };
    
    int N, M;
    struct record r[1001];

    int convert_min(int d, int h, int m)
    {
        return d * 24 * 60 + h * 60 + m;
    }

    int compare_user(const void *p,const void *q)
    {
        return strcmp(((struct record*)p)->user, ((struct record*)q)->user);
    }
    
    int compare_min(const void *p, const void *q)
    {
        return ((struct record*)p)->min - ((struct record*)q)->min;
    }

    int main(void)
    {
        for (int i = 0; i < 24; i++)
            scanf("%d", &fare[i]);
        scanf("%d", &N);
        for (int i = 0; i < N; i++)
        {
            int d, h, m;
            char line[10];
            scanf("%s %d:%d:%d:%d %s", r[i].user, &M, &d, &h, &m, line);
            r[i].min = convert_min(d, h, m);
            if (strcmp(line, "on-line") == 0)
                r[i].line_char = ONLINE;
            else
                r[i].line_char = OFFLINE;
        }
        qsort(r, N, sizeof(struct record), compare_user);
        int begin = 0;
        char* temp = r[begin].user;
        int dayfare = 0;
        for (int i = 0; i < 24; i++)
            dayfare += fare[i];
        for (int i = 0; i <= N; )
        {       
            if (strcmp(temp, r[i].user) != 0)
            {
                qsort(r + begin, i - begin, sizeof(struct record), compare_min);
                int flag = 0;
                for (int n = begin; n < i; )
                {
                    if (r[n].line_char == ONLINE && r[n + 1].line_char == OFFLINE && n + 1 < i)
                    {
                        flag = 1;
                        n += 2;
                    }
                    else n++;
                }
                if (flag)
                {
                    printf("%s %02d\n", r[begin].user, M);
                    int sum = 0;
                    for (int n = begin; n < i; )
                    {
                        if (r[n].line_char == ONLINE && r[n + 1].line_char == OFFLINE && n + 1 < i)
                        {
                            printf("%02d:%02d:%02d", r[n].min / (24 * 60), r[n].min % (24 * 60) / 60, r[n].min % (24 * 60) % 60);
                            printf(" %02d:%02d:%02d", r[n + 1].min / (24 * 60), r[n + 1].min % (24 * 60) / 60, r[n + 1].min % (24 * 60) % 60);
                            printf(" %d", r[n + 1].min - r[n].min);
                            int mon = 0;
                            int start = r[n].min; int end = r[n + 1].min;
                            while (start != end )
                            {
                                mon += fare[(start % (60 * 24) / 60)];
                                start++;
                            }
                            sum += mon;
                            printf(" $%0.2f\n", mon / 100.0);
                            n += 2;
                        }
                        else n++;
                    }
                    printf("Total amount: $%0.2f\n", sum / 100.0);
                }
                begin = i;
                temp = r[begin].user;
            }
            else i++;
        }
        system("pause");
    }

Reference blog

Guess you like

Origin www.cnblogs.com/z-y-k/p/11574056.html