Phone Bills

1016. Phone Bills (25)


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
本题并不难,但是非常坑。
只要对每一条记录排序,并计算找出每一对"on-line"和"off-line"就行了。
过程和计算非常烦琐,而且对于没有有效订单的顾客不打印其信息,这里题目没有明确给出,因此整个题目需要花费大量的时间,不建议尝试编写。
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>

using namespace std;

struct Record {
	string name;
	string time;
	string state;
	int month;
	int date;
	int hour;
	int minute;
	void setTime() {
		month = (time[0] - '0') * 10 + time[1] - '0';
		date = (time[3] - '0') * 10 + time[4] - '0';
		hour = (time[6] - '0') * 10 + time[7] - '0';
		minute = (time[9] - '0') * 10 + time[10] - '0';
	}
};

void insertionSort(int index);
bool compare(int x, int y);
void swap(int x, int y);
void printTime(string time);
int lastingTime(Record begin, Record end);
double chargeOf(Record begin, Record end);


vector<Record> records;
vector<double> toll;
int n;
double fullDayCharge;

int main() {
	fullDayCharge = 0;
	toll.resize(24);
	for (int i = 0;i < 24;i++) {
		cin >> toll[i];
		fullDayCharge += 60 * toll[i] / 100;
	}

	cin >> n;
	records.resize(n);
	for (int i = 0;i < n;i++) {
		cin >> records[i].name >> records[i].time >> records[i].state;
		records[i].setTime();
		insertionSort(i);
	}

	string name;
	Record tmp;
	double charge, totalAmount = 0;
	bool online = false;
	bool print = false;
	for (int i = 0;i < n;i++) {
		if (records[i].name != name) {
			if (i > 0 && print)
				cout << "Total amount: $" << setprecision(2) << fixed << totalAmount << endl;

			name = records[i].name;
			totalAmount = 0;
			online = false;
			print = false;
		}

		if (records[i].state == "on-line") {
			tmp = records[i];
			online = true;
		}
		else if (records[i].state == "off-line" && online) {
			if (!print) {
				cout << name << ' ';
				cout << setw(2) << setfill('0') << records[i].month << endl;
				print = true;
			}
			online = false;
			printTime(tmp.time);
			printTime(records[i].time);
			cout << lastingTime(tmp, records[i]) << ' ';
			charge = chargeOf(tmp, records[i]);
			cout << '$' << setprecision(2) << fixed << charge << endl;
			totalAmount += charge;
		}
		//cout << records[i].name << ' ' << records[i].time << ' ' << records[i].state << endl;
	}
	if (print)
		cout << "Total amount: $" << setprecision(2) << fixed << totalAmount << endl;

	system("pause");
	return 0;
}

void insertionSort(int index) {
	for (int i = index;i > 0;i--) {
		if (compare(i - 1, i))
			swap(i - 1, i);
		else
			break;
	}
}
bool compare(int x, int y) {
	if (records[x].name > records[y].name)
		return true;
	else if (records[x].name < records[y].name)
		return false;
	else {
		if (records[x].time > records[y].time)
			return true;
		else
			return false;
	}
}
void swap(int x, int y) {
	Record tmp;
	tmp = records[x];
	records[x] = records[y];
	records[y] = tmp;
}
void printTime(string time) {
	for (int i = 3;i < 11;i++)
		cout << time[i];
	cout << ' ';
}
int lastingTime(Record begin, Record end) {
	int minute = 0;
	minute += (end.date - begin.date) * 24 * 60;
	minute += (end.hour - begin.hour) * 60;
	minute += end.minute - begin.minute;
	return minute;
}
double chargeOf(Record begin, Record end) {
	double chargeBegin = 0, chargeEnd = 0, chargeDay = 0;

	for (int i = 0;i < begin.hour;i++)
		chargeBegin += 60 * toll[i] / 100;
	chargeBegin += begin.minute*toll[begin.hour] / 100;

	for (int i = 0;i < end.hour;i++)
		chargeEnd += 60 * toll[i] / 100;
	chargeEnd += end.minute*toll[end.hour] / 100;

	chargeDay = (end.date - begin.date)*fullDayCharge;
	
	return chargeDay + chargeEnd - chargeBegin;
}

优化这个sql SELECT count( 1 ) FROM ( SELECT B.ID, B.PURCHASE_REQUEST_ID, B.MATERIAL_ID, B.MATERIAL_CODE, B.MATERIAL_NAME, B.STANDARD, B.MODEL_ID, B.BILL_ROW_ID, B.BILL_NO, BILL_NAME, B.MODEL_CODE, B.MODEL_NAME, B.PARENT_MODEL_ID, B.PARENT_MODEL_CODE, B.PARENT_MODEL_NAME, B.UNIT_CODE, B.UNIT_NAME, B.PURCHASE_TYPE_CODE, CAST( NVL( B.APPLY_NUM, 0 ) AS NUMBER ( 24, 10 ) ) AS APPLY_NUM, CAST( NVL( B.DEAL_NUM, 0 ) AS NUMBER ( 24, 10 ) ) AS DEAL_NUM, CAST( NVL( B.RETURN_NUM, 0 ) AS NUMBER ( 24, 10 ) ) AS RETURN_NUM, B.DEAL_USER_ID, B.DEAL_USER_NAME, CAST( NVL( B.PRICE, 0 ) AS NUMBER ( 24, 10 ) ) AS PRICE, CAST( NVL( B.AMOUNT, 0 ) AS NUMBER ( 24, 10 ) ) AMOUNT, B.IMPLEMENT_CODE, B.IMPLEMENT_NAME, B.IMPLEMENT_INVEST_AMOUNT, B.PURCHASE_MANAGER_ID, B.PURCHASE_MANAGER_NAME, B.PROVIDER_ID, B.PROVIDER_NAME, B.REMARK, B.DELIVER_AREA, B.DELIVER_ADDRESS, B.RECEIVE_PEOPLE, B.RECEIVE_PEOPLE_PHONE, B.ITEM_STATUS, B.COST_CENTER, B.COST_BUDGET_CODE, B.COST_IMPLEMENT_NAME, B.FRAME_CONT_ID, B.FRAME_CONT_CODE, B.FRAME_CONT_NAME, B.DETAIL_CONFIG, B.PURCHASE_CATEGORY_CODE, B.INVOICE_TITLE_CODE, B.INVOICE_SEND_ADDRRSS, B.MATERIAL_REQUEST_ITEM_ID, B.YEAR, B.DELETE_FLAG, B.PROVINCE_CODE, B.REASON, B.PARENT_ITEM_ID, B.FRAME_CONT_ITEM_ID, B.SUB_MATERIAL_REQUEST_ID, B.SUB_MATERIAL_REQUEST_CODE, B.MATERIAL_URL, B.RECOMMEND_PROVIDER_NAMES, C.PURCHASE_REQUEST_CODE, C.PURCHASE_REQUEST_NAME, C.APPLY_TYPE_CODE, C.CREATOR_NAME, C.APPLY_TELEPHONE, C.COMPANY_NAME, C.DEPT_NAME, B.CREATE_TIME, TO_CHAR( B.CREATE_TIME, 'YYYY-MM-DD' ) CREATE_TIME_STR, C.ARRIVE_TIME, C.IS_TO_END, C.MONEY_WAY_CODE, C.OWN, C.APPLY_CATEGORY_CODE, C.manu_Type, C.BILL_ID, MMD.MATERIAL_TYPE_CODE, B.BRANCH_COMPANY_DEAL_USER_ID, B.BRANCH_COMPANY_DEAL_USER_NAME, ( SELECT ORG_NAME FROM ORGANIZATIONS WHERE DELETE_FLAG = '0' AND ORG_CODE = ( SELECT PARENT_COMPANY_NO FROM ORGANIZATIONS WHERE ID = B.MATERIAL_DEPT_ID )) AS MATERIAL_COMPANY_NAME, B.ORIGINAL, B.PROVIDER_PRODUCT_MODEL, B.PROVIDER_PRODUCT_NAME, B.PRODUCT_DESC, B.Back_Flag, CASE WHEN MMD.material_type_code = 'WZ' THEN '1' WHEN MMD.material_type_code = 'FW' THEN '2' ELSE '3' END apply_category_code_item, NVL( C.IS_CARDSYSTEM_REQUEST, '0' ) IS_CARDSYSTEM_REQUEST, B.APPLY_GROUP_AUTHORITES, B.SCIENTIFIC_RESEARCH_ID, B.SCIENTIFIC_RESEARCH_CODE, B.SCIENTIFIC_RESEARCH_NAME, B.PREQUALFY_CODE, nvl( C.IS_QUICK, '0' ) AS IS_QUICK, C.PURCHASE_WAY_CODE, C.PURCHASE_TYPE_CODE PURCHASE_TYPE_CODE_P, C.ORIGINAL_TYPE, C.PURCHASE_REQUEST_BILLS_TYPE, B.IS_FRAME_CONT_MONAD FROM PURCHASE_REQUEST_ITEM B LEFT JOIN PURCHASE_REQUEST C ON B.PURCHASE_REQUEST_ID = C.ID LEFT JOIN MATERIAL_DATA MMD ON MMD.ID = B.MATERIAL_ID AND MMD.DELETE_FLAG = '0' WHERE B.delete_flag = '0' AND B.Item_Status IN ( 1 ) AND NOT EXISTS ( SELECT * FROM purchase_request_item_log pril WHERE B.id = pril.purchase_request_item_id AND pril.lock_status = '1' AND pril.delete_flag = '0' ) AND ( ( c.apply_type_code NOT IN ( '20', '41', '3' ) AND nvl( B.Apply_Num, 0 ) > nvl( B.Deal_Num, 0 )) OR c.apply_type_code IN ( '20', '41', '3' ) ) AND B.Deal_User_Id =: 1 AND C.MONEY_WAY_CODE =: 2 AND C.APPLY_TYPE_CODE =: 3 AND C.PAY_OUT_TYPE_CODE =: 4 AND C.APPLY_CATEGORY_CODE =: 5 AND NVL( C.IS_CARDSYSTEM_REQUEST, '0' ) = : 6 AND NOT EXISTS ( SELECT * FROM purchase_request_item p left join material_province mp ON p.material_id = mp.material_id WHERE p.delete_flag = 0 AND mp.delete_flag = 0 AND mp.material_status = 03 AND mp.org_code = p.province_code AND p.id = B.id ) ORDER BY C.ID, B.ID ASC)
06-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值