pat 1016 Phone Bills --java解题分析

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个非负整数组成,表示从00:00 - 01:00的通行费(美分/分钟),从01:00 - 02:00的通行费,等等。
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.
下一行包含正数N(≤1000),后面是N行记录。每条通话记录由客户名称(不包含空格,长度不超过20个字符)、时间日期(MM:dd:HH: MM)、单词online或offline组成。

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.
对于每个测试用例,所有日期都将在一个月内。每条在线记录都与同一客户的下一条记录按时间顺序配对,前提是它是离线记录。任何没有与脱机记录配对的联机记录都被忽略,与没有与联机记录配对的脱机记录一样。它保证至少有一个调用在输入中配对良好。您可以假设同一个客户没有两条记录具有相同的时间。时间是用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.
帐单必须按客户姓名的字母顺序打印。对于每个客户,首先按照示例所示的格式在一行中打印客户的姓名和账单的月份。然后,对于呼叫的每个时间段,在一行中打印开始和结束的时间和日期(dd:HH:mm)、持续时间(分钟)和呼叫费用。电话必须按时间顺序列出。最后,按样例显示的格式打印当月的总费用。

在这里插入图片描述

/**
原题分析:
第一部分:时段费用 10 10 10 10 10 10 20 20 20 15 15 15 15 15 15 15 20 30 20 15 15 10 10 10  
                 0  1  2  3  4  5  6  7  8  9  10 11 12 13 14 15 16 17 18 19 20 21 22 23
                 可以使用数组,长度24

第二部分:名字长度不超过20 日期格式MM:dd:HH:mm 开始拨打 - 结束通话
                 切割 排序:条件时间小的在前面(因有通话就有结束)  
CYJJ 01:01:05:59 on-line   五点到六点费用为 10*0.01*1
CYJJ 01:01:07:00 off-line  六点到七点费用为 20*0.01*60

第三部分:账单格式为 (用户名+" "+月份) (开始[日+时+分]+" "+结束[日+时+分]+费用) (Total amount:+账单费用)

CYJJ 01 
01:05:59 01:07:00 61 $12.10    = (7:00-05:59)
Total amount: $12.10

CYLL 01:01:06:01 on-line
CYLL 01:01:08:03 off-line
CYLL 01:28:15:41 on-line
CYLL 01:28:16:05 off-line

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:01:01:03 on-line
aaa 01:02:00:01 on-line
aaa 01:05:02:24 on-line
aaa 01:04:23:59 off-line

aaa 01
02:00:01 04:23:59 4318 $638.80
Total amount: $638.80
**/
import java.util.*;
//又是一个可悲的十五分,运行通过,题目测试用例通过,idea运行通过

public class Main {
    // 9个10   5个20  1个30  9个15
    public static int[] timeMoney = new int[24];
    public static Set<String> set = new LinkedHashSet<>();  //利用不重复进行用户名存储
    public static Set<String> strings = new TreeSet<>(); //利用集合排序
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        //输入费率
        for (int i = 0; i < 24; i++) {
            timeMoney[i] = scanner.nextInt();
        }
        int i = scanner.nextInt(); //总账单记录
        int arrNum = 0; //用于数组定位
        String[] str = new String[i + 1];
        String[] ret = new String[i/2];
        for (int j = 0; j <= i; j++) {
            String s = scanner.nextLine();  //输入账单
            strings.add(s);
        }
        //使用迭代器取出放入数组
        Iterator<String> iterator = strings.iterator();
        while (iterator.hasNext()) {
            str[arrNum] = iterator.next();
            arrNum++;
        }
        str[0] = "This is null";
        //开始拆分字符串
        //第一轮为:用户名 + 日期时间 + 开关标识
        String username;
        String bill;
        double couMoney;
        int couDay;
        int arrSum = 0;
        for (int j = 0; j < str.length - 1; j++) {
            //以测试aaa例子前提先验证第二条是否为off
            String[] offLine = str[j + 1].split(" ");
            if (offLine[2].equals("off-line")) {
                String[] onLine = str[j].split(" ");
                //第二轮拆分时间 : MM:dd:HH:mm
                String[] offTime = offLine[1].split(":"); //拆分出来的结束通话时间值
                String[] onTime = onLine[1].split(":"); //拆分出来的开始通话时间值

                int offDay = Integer.valueOf(offTime[1]);  //结束日期
                int offHour = Integer.valueOf(offTime[2]); //结束时间
                int offMinute = Integer.valueOf(offTime[3]);  //结束分钟
                int onDay = Integer.valueOf(onTime[1]);  //开始日期
                int onHour = Integer.valueOf(onTime[2]); //开始时间
                int onMinute = Integer.valueOf(onTime[3]); //结束分钟

                couMoney = sumMoney(offDay, offHour, offMinute, onDay, onHour, onMinute);
                couDay = sumMinute(offDay, offHour, offMinute, onDay, onHour, onMinute);

                String format = String.format("%.2f", couMoney);
                //结果叠加
                StringBuffer stringBuffer = new StringBuffer();
                StringBuffer append = stringBuffer.append(offLine[0] + " " + offTime[0] + " " + onTime[1] + ":" + onTime[2] + ":" + onTime[3] + " " + offTime[1] + ":" + offTime[2] + ":" + offTime[3] + " " + couDay + " " + format);
                //将结果存放数组中
                ret[arrSum] = append.toString();  //存放过滤后的数据
                set.add(onLine[0]+" "+onTime[0]); //存放过滤后的用户名和月份
                arrSum++;
            }
        }
        //再次拆分: 分别拆成 :  arr[0]:username  arr[1]:MM  arr[2]: on(dd:HH:mm)  arr[3]: off(dd:HH:mm)  arr[4]: couDay  arr[5]: money
        //CYJJ 01 01:05:59 01:07:00 61 12.1
        //CYLL 01 01:06:01 01:08:03 122 24.4
        //CYLL 01 28:15:41 28:16:05 24 3.85
        //aaa 01 02:00:01 04:23:59 4318 638.8
        double couMon = 0d;
        int count = 0;
        for (String s : set) {
            System.out.println(s);
            for (int j = 0+count; j < ret.length-1; j++) {
                String[] sName = s.split(" ");
                String[] retSpl = ret[j].split(" ");
                if (boolRet(sName[0].toCharArray(),retSpl[0].toCharArray())){
                    couMon += Double.valueOf(retSpl[5]);
                    System.out.println(retSpl[2]+" "+retSpl[3]+" "+retSpl[4]+" $"+retSpl[5]);
                }
            }
            String endBill = String.format("%.2f", couMon);
            System.out.println("Total amount: $"+endBill);
            couMon=0;
            count++;
        }

    }

    /**
     *判断用户是否相同
     */
    private static boolean boolRet(char[] chars1, char[] chars2) {
        int count1 = 0;
        int count2 = 0;
        for (int i = 0; i < chars1.length; i++) {
            count1 += chars1[i] - 'A';
        }
        for (int i = 0; i < chars2.length; i++) {
            count2 += chars2[i] - 'A';
        }
        return count1==count2?true:false;
    }

    /**
     * 计算时长
     */
    private static int sumMinute(int offDay, int offHour, int offMinute, int onDay, int onHour, int onMinute) {
        int cou = 0;
        cou +=  onDay != offDay? (offDay - onDay) * 24 * 60:0;
        //计算小时
        int minute = onMinute;
        for (int x = onHour; x < offHour; x++) {
            cou += 60 - minute;
            minute = 0;
        }
        cou += offMinute;
        return cou;
    }

    /**
     * 返回一个金额
     */
    private static double sumMoney(int offDay, int offHour, int offMinute, int onDay, int onHour, int onMinute) {
        double countMoney = 0d; //统计金额
        countMoney += onDay != offDay? (offDay - onDay) * 213:0; //一天总费用为213
        //计算小时
        int minute = onMinute;
        for (int x = onHour; x < offHour; x++) {
            countMoney += timeMoney[x] * 0.01 * (60 - minute);
            minute = 0;
        }
        //计算最后一小时
        countMoney += timeMoney[offHour] * 0.01 * offMinute;
        return countMoney;
    }

}


测试结果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值