# 程序控

IPPP (Institute of Penniless Peasent-Programmer) Fellow

:: :: :: :: :: :: :: ::
 77 随笔 :: 0 文章 :: 457 评论 :: 0 引用

Time limit: 3.000 seconds

## Problem问题

Gondwanaland Telecom makes charges for calls according to distance and time of day. The basis of the charging is contained in the following schedule, where the charging step is related to the distance:

 Charging Step(distance) Day Rate8am to 6pm Evening Rate6pm to 10pm Night Rate10pm to 8am A 0.10 0.06 0.02 B 0.25 0.15 0.05 C 0.53 0.33 0.13 D 0.87 0.47 0.17 E 1.44 0.80 0.30

All charges are in dollars per minute of the call. Calls which straddle a rate boundary are charged according to the time spent in each section. Thus a call starting at 5:58 pm and terminating at 6:04 pm will be charged for 2 minutes at the day rate and for 4 minutes at the evening rate. Calls less than a minute are not recorded and no call may last more than 24 hours.

Write a program that reads call details and calculates the corresponding charges.

## Input and Output输入与输出

Input lines will consist of the charging step (upper case letter 'A'..'E'), the number called (a string of 7 digits and a hyphen in the approved format) and the start and end times of the call, all separated by exactly one blank. Times are recorded as hours and minutes in the 24 hour clock, separated by one blank and with two digits for each number. Input will be terminated by a line consisting of a single #.

Output will consist of the called number, the time in minutes the call spent in each of the charge categories, the charging step and the total cost in the format shown below.

## Sample Input输入示例

A 183-5724 17 58 18 04
#

## Sample Output输出示例

0         10        20        30
123456789012345678901234567890123456789

183-5724     2     4     0  A    0.44

## Solution解答

#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
//按照指定的开始和起始时间统计各时间段内的分钟数
void StepStatic(int nBeg, int nEnd, int *pCate, int nCnt, int *pResult) {
int ic= 0; //ic表示当前时段，下面的循环找到开始时间所在时段
for (; ic < nCnt - 1 && nBeg >= pCate[ic]; ++ic);
//开始时间的时段减去开始前的无通话时间
pResult[ic - 1] -= (nBeg - pCate[ic - 1]);
//循环统计每个时段的时间
for (--ic; ic < nCnt - 1 && nEnd >= pCate[ic]; ++ic) {
pResult[ic] += pCate[ic + 1] - pCate[ic];
}
//结束时间所在时段减去结束后的无通话时间
pResult[ic - 1] -= (pCate[ic] - nEnd);
}
int main(void) {
int Cate[] = {0, 8 * 60, 18 * 60, 22 * 60, 24 * 60}; //时间段分钟数
const int nCnt = sizeof(Cate) / sizeof(Cate[0]); //时间段数
int Rate[5][3] = { {10, 6, 2}, {25, 15, 5}, {53, 33, 13},
{87, 47, 17}, {144, 80, 30} }; //费率表
cout.setf(ios_base::right); //输出格式居右对齐
//循环处理每一行输入的数据，直到出现#号为止
for (char cStep; cin >> cStep && cStep != '#'; cout << endl) {
string Num;
int BegH, BegM, EndH, EndM, nCharge = 0, Result[nCnt - 1] = {0};
cin >> Num >> BegH >> BegM >> EndH >> EndM; //读入数据
int nBeg = BegH * 60 + BegM, nEnd = EndH * 60 + EndM;
if (nEnd <= nBeg) { //如果开始时间小于或等于结束时间
//先统计从开始时间到24点，再从0点到结束时间
StepStatic(nBeg, Cate[nCnt - 1], Cate, nCnt, Result);
StepStatic(0, nEnd, Cate, nCnt, Result);
}
else { //否则直接统计从开始时间到结束时间
StepStatic(nBeg, nEnd, Cate, nCnt, Result);
}
//将最后到24点的时段和从0点开始的时段合并
Result[nCnt - 2] += Result[0];
cout << setw(10) << Num; //输出电话号码
for (int i = 1; i < nCnt - 1; ++i) { //统计话费并输出各时段话时
nCharge += Rate[cStep - 'A'][i - 1] * Result[i];
cout << setw(6) << Result[i];
}
cout << setw(3) << cStep; //输出距离及总话费
cout << setw(8) << fixed << setprecision(2) << (nCharge / 100.0f);
}
return 0;
}


posted on 2010-09-06 07:46  Devymex  阅读(635)  评论(0编辑  收藏