PAT 1016. Phone Bills (25)(map排序,去掉不匹配的,分时计算money)(待修改)

题目

1016. Phone Bills (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue
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.10Totalamount: 12.10
CYLL 01
01:06:01 01:08:03 122 24.4028:15:4128:16:0524 3.85
Total amount: 28.25aaa0102:00:0104:23:594318 638.80
Total amount: $638.80

解题思路

  • 1.map排序,去掉不匹配的,分时计算money(具体看下面代码)。

代码(支队了测试用例,段错误,以后再改)

#include<cstdio>
#include<algorithm>
#include<vector>
#include<string>
#include<map>
#include<iostream>
#include<set>
#include<deque>
#include<iomanip>
using namespace std;
int price[24];
int n,dayMoney=0;
struct rec
{
    int month,day,hh,mm;
    string inout;
    rec(){}
    rec(int _month,int _day,int _hh,int _mm,string _inout){
        this->month = _month;
        this->day = _day;
        this->hh = _hh;
        this->mm = _mm;
        this->inout = _inout;
    }
};
struct t{
    int day,hh,mm;
    t(){}
    t(int _day,int _hh,int _mm){
        this->day = _day;
        this->hh = _hh;
        this->mm = _mm;
    }
};
int countTime(t a,t b){
    return   b.day*24*60 +b.hh*60 + b.mm - (a.day*24*60 +a.hh*60 + a.mm);
}
int countMoney(t in,t out){
    int sum = 0;
    if (in.day == out.day) {
        sum += (60 - in.mm) * price[in.hh];
        for (int i = in.hh + 1; i < out.hh; ++i) {
            sum += price[i] * 60;
        }
        sum += out.mm* price[out.hh];
    }else {
        //第一天的时间
        sum += (60 - in.mm) * price[in.hh];
        for (int i = in.hh + 1; i < 24; ++i) {
            sum += 60 * price[i];
        }
        //中间天的时间
        sum += (out.day - in.day - 1)*dayMoney;
        //最后一天的时间
        for (int i = 0; i < out.hh; ++i) {
            sum += price[i]*60;
        }
        sum += out.mm * price[out.hh];
    }
    return sum;
}

bool cmp(const rec & a,const rec & b){
    return a.day*24*60 +a.hh*60 + a.mm < b.day*24*60 +b.hh*60 + b.mm;
}

map<string,vector<rec> ,less<string> > p;
int main(int argc, char *argv[])
{
    for (int i = 0; i < 24; ++i) {
        scanf("%d",&price[i]);
        dayMoney +=price[i]*60;
    }
    scanf("%d",&n);
    string _name,_inout;
    int _month,_day,_hh,_mm;
    for (int i = 0; i < n; ++i) {
        cin >> _name;
        scanf("%d:%d:%d:%d",&_month,&_day,&_hh,&_mm);
        cin >> _inout;
        p[_name].push_back(rec(_month,_day,_hh,_mm,_inout));
    }
    //取出一个人的记录并计算
    map<string,vector<rec> >::iterator it;
    for (it = p.begin(); it !=p.end(); ++it) {
        double totalMoney = 0;//totalTime = 0;
        //vector<rec> tem = p["aaa"];
        vector<rec> tem = it->second;
        cout << it->first << " " ;
        printf("%02d\n",tem[0].month);
        sort(tem.begin(),tem.end(),cmp);
        deque<rec> deq;

        for (int i = 0; i < tem.size(); ++i) {
            //如果来的是on-line
            if (tem[i].inout[1] == 'n') {
                // 空 或者 off
                if (deq.empty()||deq.back().inout[1] == 'f') {
                    deq.push_back(tem[i]);
                }
                // on
                else if (!deq.empty()&&deq.back().inout[1] == 'n') {
                    deq.pop_back();
                    deq.push_back(tem[i]);
                }
            }
            //如果来的是off-line
            else {
                rec tem_back = deq.back();
                //如果匹配成功了就可以计算了
                if(!tem.empty()&&tem_back.inout[1] == 'n'){
                    t in_time = t(tem_back.day,tem_back.hh,tem_back.mm);
                    t out_time = t(tem[i].day,tem[i].hh,tem[i].mm);
                    int tem_time = countTime(in_time,out_time);
                    double tem_money = countMoney(in_time,out_time) / 100.00;
                    totalMoney += tem_money;
                    printf("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",in_time.day,in_time.hh,in_time.mm,out_time.day,out_time.hh,out_time.mm,tem_time,tem_money);
                    deq.push_back(tem[i]);
                }
            }
        }
        printf("Total amount: $%.2f\n",totalMoney);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
优化这个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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值