[PAT]1016. Phone Bills (25)@Java

来源:互联网 发布:怎样删除精选淘宝 编辑:程序博客网 时间:2024/06/16 13:21

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 1010CYLL 01:01:06:01 on-lineCYLL 01:28:16:05 off-lineCYJJ 01:01:07:00 off-lineCYLL 01:01:08:03 off-lineCYJJ 01:01:05:59 on-lineaaa 01:01:01:03 on-lineaaa 01:02:00:01 on-lineCYLL 01:28:15:41 on-lineaaa 01:05:02:24 on-lineaaa 01:04:23:59 off-line
Sample Output:
CYJJ 0101:05:59 01:07:00 61 $12.10Total amount: $12.10CYLL 0101:06:01 01:08:03 122 $24.4028:15:41 28:16:05 24 $3.85Total amount: $28.25aaa 0102:00:01 04:23:59 4318 $638.80Total amount: $638.80




package go.jacob.day920;import java.text.DecimalFormat;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.Scanner;/** * 1016. Phone Bills (25) *  * @author Jacob 1.首先建立相应的数据结构 * */public class Demo1 {static int[] hourCost;public static void main(String[] args) {Scanner sc = new Scanner(System.in);// 记录每个时间段的花费hourCost = new int[24];for (int i = 0; i < 24; i++) {hourCost[i] = sc.nextInt();}int n = sc.nextInt();// 数字输出格式DecimalFormat df = new DecimalFormat("00");DecimalFormat df1 = new DecimalFormat(".00");// 读取每条记录,并存储在相应数据结构中ArrayList<Call> allCalls = new ArrayList<Call>();for (int i = 0; i < n; i++) {Call tmp = new Call();tmp.name = sc.next();String time = sc.next();String[] times = time.split(":");tmp.month = Integer.parseInt(times[0]);tmp.date = Integer.parseInt(times[1]);tmp.hour = Integer.parseInt(times[2]);tmp.minute = Integer.parseInt(times[3]);tmp.status = sc.next();allCalls.add(tmp);}// 对所有的元素进行排序MyComparator compare = new MyComparator();Collections.sort(allCalls, compare);// 删除无效记录ArrayList<Call> formatCalls = new ArrayList<Call>();for (int i = 0; i < allCalls.size(); i++) {Call curCall = allCalls.get(i);// 判断call能否真正放入formatsolve(formatCalls, curCall);// 还要判断最后一个是否为on-lineif (i == allCalls.size() - 1) {Call tmp = formatCalls.get(formatCalls.size() - 1);if (tmp.status.equals("on-line"))formatCalls.remove(formatCalls.size() - 1);}}// 现在format中的Call元素都是合法配对的数据double sum = 0;for (int i = 0; i < formatCalls.size(); i += 2) {if (i == 0) {System.out.println(formatCalls.get(i).name + " " + df.format(formatCalls.get(i).month));}// 如果i不为0,且与上一call不属于同一个人if (i != 0 && !formatCalls.get(i).name.equals(formatCalls.get(i - 1).name)) {System.out.println("Total amount: $" + df1.format(sum / 100));System.out.println(formatCalls.get(i).name + " " + df.format(formatCalls.get(i).month));sum = 0;}Call cur = formatCalls.get(i), next = formatCalls.get(i + 1);double[] res = calTime(cur, next);sum += res[1];System.out.println(df.format(cur.date) + ":" + df.format(cur.hour) + ":" + df.format(cur.minute) + " "+ df.format(next.date) + ":" + df.format(next.hour) + ":" + df.format(next.minute) + " "+ df.format(res[0]) + " $" + df1.format(res[1] / 100));}System.out.println("Total amount: $" + df1.format(sum / 100));sc.close();}private static double[] calTime(Call cur, Call next) {// res[0]为所花费时间 res[1]为所花费的钱double[] res = new double[2];res[0] = next.getTotal() - cur.getTotal();res[1] = getMoney(next.getTotal()) - getMoney(cur.getTotal());return res;}// 计算从月初经历total分钟的花费private static int getMoney(int total) {int hours = total / 60;int minutes = total % 60;int sum = 0;for (int i = 0; i < hours; i++) {sum += hourCost[i % 24] * 60;}sum += hourCost[hours % 24] * minutes;return sum;}/* * 根据preCall和curCall来生成foamat集合 */private static void solve(ArrayList<Call> formatCalls, Call curCall) {if (formatCalls.isEmpty()) {// 如果集合为空,只有当当前为on-line时才能放入集合if (curCall.status.equals("on-line")) {formatCalls.add(curCall);}return;}Call preCall = formatCalls.get(formatCalls.size() - 1);// 如果是同一个人的通话记录if (preCall.name.equals(curCall.name)) {if (preCall.status.equals("on-line")) {// 如果两个都为on-line,保留最新的一个if (curCall.status.equals("on-line")) {formatCalls.remove(formatCalls.size() - 1);formatCalls.add(curCall);} else {// 如果当前通话记录为off-line,加入集合formatCalls.add(curCall);}} else {// preCall的status为off-line,只有当前通话记录为on-line时才加入集合if (curCall.status.equals("on-line"))formatCalls.add(curCall);}} else {// 不是同一个人的通话记录// 如果上一个人的最后一个通话记录是on-line,非法记录,删除if (preCall.status.equals("on-line"))formatCalls.remove(formatCalls.size() - 1);// 如果当前通话记录(新的一个人)为on-line,加入集合if (curCall.status.equals("on-line"))formatCalls.add(curCall);}}}class MyComparator implements Comparator<Call> {// 按名字,时间排序@Overridepublic int compare(Call c1, Call c2) {// String判断不能用==if (!c1.name.equals(c2.name))return c1.name.compareTo(c2.name);else if (c1.month != c2.month)return c1.month - c2.month;else if (c1.date != c2.date)return c1.date - c2.date;else if (c1.hour != c2.hour)return c1.hour - c2.hour;else if (c1.minute != c2.minute)return c1.minute - c2.minute;elsereturn 0;}}/* * 电话记录数据结构 */class Call {public String name;int month;int date;int hour;int minute;int total;String status;// 计算相对于月初的时间public int getTotal() {total = date * 24 * 60 + hour * 60 + minute;return total;}// 生成toString方法便于调试@Overridepublic String toString() {return "Call [name=" + name + ", month=" + month + ", date=" + date + ", hour=" + hour + ", minute=" + minute+ ", status=" + status + "]";}}


原创粉丝点击