PAT1016
A long-distance telephone company charges its customers by the following rules:
一個(gè)長(zhǎng)途電話公司費(fèi)用告訴它的顧客需要遵循以下的規(guī)則
Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made.
打一個(gè)長(zhǎng)途電話一分鐘的費(fèi)用是依據(jù)什么時(shí)候撥打這個(gè)電話計(jì)算得到的。
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.
當(dāng)一個(gè)顧客開始連接長(zhǎng)途電話的時(shí)候,時(shí)間會(huì)被記錄,直到當(dāng)用戶掛掉這個(gè)電話為止。
Every calendar month, a bill is sent to the customer for each minute called (at a rate determined by the time of day).
每個(gè)月,一個(gè)賬單會(huì)送到顧客手中對(duì)于每一分鐘撥打,頻率是以每天計(jì)算的。
Your job is to prepare the bills for each month, given a set of phone call records.
你的任務(wù)是準(zhǔn)備著每個(gè)月的賬單,給出一個(gè)電話記錄的集合。
Input Specification:
Each input file contains one test case. Each case has two parts: the rate structure, and the phone call records.
每個(gè)輸入文件包含一個(gè)測(cè)試用例。每個(gè)測(cè)試用例有兩個(gè)部分,頻率結(jié)構(gòu),和電話記錄
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.
頻率結(jié)構(gòu)包含一行,用24個(gè)正整數(shù)組成,表示每一個(gè)小時(shí)的收費(fèi)
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".
下面一行包含一個(gè)小于1000的正整數(shù),下面N行記錄,每個(gè)電話記錄包含顧客的名字,20個(gè)字符以內(nèi)沒(méi)有空格,時(shí)間和日期,和一個(gè)詞"on-line" or "off-line".
For each test case, all dates will be within a single month.
對(duì)于每個(gè)測(cè)試用例所有的日期都將會(huì)在一個(gè)單月之內(nèi)
Each "on-line" record is paired with the chronologically next record for the same customer provided it is an "off-line" record.
每個(gè)"on-line" 記錄和"off-line" 記錄是成對(duì)出現(xiàn)的,而且是按時(shí)間順序的。
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.
任何沒(méi)有與"off-line"記錄成對(duì)出現(xiàn)的"on-line" 記錄都被忽略,對(duì)于沒(méi)有"on-line" 記錄相對(duì)應(yīng)得"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.
至少會(huì)有一個(gè)電話記錄成對(duì)出現(xiàn)在輸入文件中。你可以確定沒(méi)有兩個(gè)記錄對(duì)于同一個(gè)顧客同一個(gè)時(shí)間是一模一樣的,時(shí)間記錄使用24小時(shí)制。
Output Specification:
For each test case, you must print a phone bill for each customer.
對(duì)于每個(gè)測(cè)試用例你需要打印出每個(gè)用戶的電話賬單
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.
對(duì)于每個(gè)用戶,先打印一行名字,然后一個(gè)月的賬單格式如下。
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.
對(duì)于每個(gè)時(shí)間的電話,打印出一行開始和結(jié)束的時(shí)間和日期,最后的時(shí)間和費(fèi)用。電話必須以時(shí)間順序的,最后打印一個(gè)這個(gè)月的總費(fèi)用。
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-lineSample 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?
?
這道題主要問(wèn)題死在題目中說(shuō)了 It is guaranteed that at least one call is well paired in the input. 但是如果這個(gè)人沒(méi)有任何記錄是匹配的,那么這個(gè)人是不能輸出任何信息的,也就是沒(méi)有他這個(gè)人的賬單。
?
主要利用排序,其他都是邏輯上面比較繁瑣而已。
?
下面補(bǔ)充qsort中的使用方法。題目就只是考了這個(gè)而已,沒(méi)有別的算法。
?
七種qsort排序方法
<本文中排序都是采用的從小到大排序>
一、對(duì)int類型數(shù)組排序
int num[100];
Sample:
int cmp ( const void *a , const void *b )
{
return *(int *)a - *(int *)b;
}
qsort(num,100,sizeof(num[0]),cmp);
二、對(duì)char類型數(shù)組排序(同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);
三、對(duì)double類型數(shù)組排序(特別要注意)
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);
四、對(duì)結(jié)構(gòu)體一級(jí)排序
struct In
{
double data;
int other;
}s[100]
//按照data的值從小到大將結(jié)構(gòu)體排序,關(guān)于結(jié)構(gòu)體內(nèi)的排序關(guān)鍵數(shù)據(jù)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);
五、對(duì)結(jié)構(gòu)體二級(jí)排序
struct In
{
int x;
int y;
}s[100];
//按照x從小到大排序,當(dāng)x相等時(shí)按照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);
六、對(duì)字符串進(jìn)行排序
struct In
{
int data;
char str[100];
}s[100];
//按照結(jié)構(gòu)體中字符串str的字典順序排序
int cmp ( const void *a , const void *b )
{
return strcmp( (*(In *)a)->str , (*(In *)b)->str );
}
qsort(s,100,sizeof(s[0]),cmp);
七、計(jì)算幾何中求凸包的cmp
int cmp(const void *a,const void *b) //重點(diǎn)cmp函數(shù),把除了1點(diǎn)外的所有點(diǎn),旋轉(zhuǎn)角度排序
{
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)) //如果在一條直線上,則把遠(yuǎn)的放在前面
return 1;
else return -1;
}
PS:
其中的qsort函數(shù)包含在<stdlib.h>的頭文件里,strcmp包含在<string.h>的頭文件里
轉(zhuǎn)載于:https://www.cnblogs.com/linkstar/p/5758570.html
總結(jié)
- 上一篇: 【EF学习笔记09】----------
- 下一篇: 敏捷的最佳实践-2