PAT1016

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.

频率结构包含一行,用24个正整数组成,表示每一个小时的收费

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".

下面一行包含一个小于1000的正整数,下面N行记录,每个电话记录包含顾客的名字,20个字符以内没有空格,时间和日期,和一个词"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.

每个"on-line" 记录和"off-line" 记录是成对出现的,而且是按时间顺序的。

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.

任何没有与"off-line"记录成对出现的"on-line" 记录都被忽略,对于没有"on-line" 记录相对应得"off-line"记录也一样。

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.

至少会有一个电话记录成对出现在输入文件中。你可以确定没有两个记录对于同一个顾客同一个时间是一模一样的,时间记录使用24小时制。

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

这道题主要问题死在题目中说了 It is guaranteed that at least one call is well paired in the input. 但是如果这个人没有任何记录是匹配的,那么这个人是不能输出任何信息的,也就是没有他这个人的账单。

主要利用排序,其他都是逻辑上面比较繁琐而已。

下面补充qsort中的使用方法。题目就只是考了这个而已,没有别的算法。

七种qsort排序方法

<本文中排序都是采用的从小到大排序>

一、对int类型数组排序

int num[100];

Sample:

int cmp ( const void *a , const void *b )
{
return *(int *)a - *(int *)b;
}

qsort(num,100,sizeof(num[0]),cmp);

二、对char类型数组排序(同int类型)

char word[100];

Sample:

int cmp( const void *a , const void *b )
{
return *(char *)a - *(int *)b;
}

qsort(word,100,sizeof(word[0]),cmp);

三、对double类型数组排序(特别要注意)

double in[100];

int cmp( const void *a , const void *b )
{
return *(double *)a > *(double *)b ? 1 : -1;
}

qsort(in,100,sizeof(in[0]),cmp);

四、对结构体一级排序

struct In
{
double data;
int other;
}s[100]

//按照data的值从小到大将结构体排序,关于结构体内的排序关键数据data的类型可以很多种,参考上面的例子写

int cmp( const void *a ,const void *b)
{
return (*(In *)a).data > (*(In *)b).data ? 1 : -1;
}

qsort(s,100,sizeof(s[0]),cmp);

五、对结构体二级排序

struct In
{
int x;
int y;
}s[100];

//按照x从小到大排序,当x相等时按照y从大到小排序

int cmp( const void *a , const void *b )
{
struct In *c = (In *)a;
struct In *d = (In *)b;
if(c->x != d->x) return c->x - d->x;
else return d->y - c->y;
}

qsort(s,100,sizeof(s[0]),cmp);

六、对字符串进行排序

struct In
{
int data;
char str[100];
}s[100];

//按照结构体中字符串str的字典顺序排序

int cmp ( const void *a , const void *b )
{
return strcmp( (*(In *)a)->str , (*(In *)b)->str );
}

qsort(s,100,sizeof(s[0]),cmp);

七、计算几何中求凸包的cmp

int cmp(const void *a,const void *b) //重点cmp函数,把除了1点外的所有点,旋转角度排序
{
struct point *c=(point *)a;
struct point *d=(point *)b;
if( calc(*c,*d,p[1]) < 0) return 1;
else if( !calc(*c,*d,p[1]) && dis(c->x,c->y,p[1].x,p[1].y) < dis(d->x,d->y,p[1].x,p[1].y)) //如果在一条直线上,则把远的放在前面
return 1;
else return -1;
}

PS:

其中的qsort函数包含在<stdlib.h>的头文件里,strcmp包含在<string.h>的头文件里

原文地址:https://www.cnblogs.com/linkstar/p/5758570.html