PAT 1016 Phone Bills 排序

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

先將所有記錄排序,排序時先按用戶名排序再按時間排序,使得同一用戶的所有記錄依次排列;然後順序處理同一個用戶的所有記錄,需要判斷各用戶是否有有效記錄;對於每個用戶的有效通話記錄根據費率計算費用(美元),並得出總費用,其中計算時長可以由起始時間不斷+1直到終止時間即可。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.Comparator;

class Record implements Comparable<Record>{
	String id;
	int mon,day,hour,min;
	boolean status;//true爲online
	public Record(String id,String time,String status) {
		this.id=id;
		String[] t=time.split(":");
		mon=Integer.valueOf(t[0]);
		day=Integer.valueOf(t[1]);
		hour=Integer.valueOf(t[2]);
		min=Integer.valueOf(t[3]);
		this.status=status.equals("on-line");
	}
	@Override
	public int compareTo(Record o) {
		if(!id.equals(o.id))//按用戶名字典序排序
			return id.compareTo(o.id);
		//同一人所有記錄按時間升序排序
		else if(day!=o.day)
			return day-o.day;
		else if(hour!=o.hour)
			return hour-o.hour;
		else 
			return min-o.min;
	}
}
public class Main {

	static int[] toll=new int[24];//記錄費率

	//處理一對記錄
	static double solve(Record r1,Record r2) {
		int d=r1.day,h=r1.hour,m=r1.min;//起始時間
		int time=0;//總時間
		double money=0;
		//計算兩條記錄時間差,不斷加一
		while(d<r2.day||h<r2.hour||m<r2.min) {
			money+=toll[h];//加上當前時間費率
			m++;time++;
			if(m==60) {
				m=0;h++;
			}
			if(h==24) {
				h=0;d++;
			}
		}
		money/=100.0;//換算成美元
		System.out.format("%02d:%02d:%02d %02d:%02d:%02d %d $%.2f\n",
				r1.day,r1.hour,r1.min,r2.day,r2.hour,r2.min,time,money);
		return money;
	}
    public static void main(String[] args) throws IOException {
    	BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));

    	String[] line=reader.readLine().split(" ");    	
    	for(int i=0;i<24;i++) {
    		toll[i]=Integer.valueOf(line[i]);
    	}
    	
    	int N=Integer.valueOf(reader.readLine());
    	Record[] records=new Record[N];
    	
    	for(int i=0;i<N;i++) {
    		line=reader.readLine().split(" ");
    		Record r=new Record(line[0], line[1], line[2]);
    		records[i]=r;
    	}
    	//排序
    	Arrays.sort(records);
    	int cur=0;//指向當前處理的用戶第一條記錄
    	while(cur<N) {
    		boolean flag=false;//標記該用戶是否有匹配記錄
    		boolean on=records[cur].status;//標記是否有online等待匹配,初值爲第一條記錄的status
    		double totalAmount=0.0;//總金額
    		int i;//檢查該用戶其他記錄
    		for(i=cur+1;i<N&&records[i].id.equals(records[cur].id);i++) {
    			Record r=records[i];
    			if(!r.status&&on==true) {//當前記錄爲offline且前面已有online等待匹配
    				if(!flag) {//該用戶第一組匹配記錄
    					System.out.println(r.id+" "+String.format("%02d", r.mon));
    					flag=true;
    				}
    				totalAmount+=solve(records[i-1],r);
    				on=false;
    			}else if(r.status) {//當前記錄爲online,不處理
    				on=true;
    			}
    		}
    		if(flag) {//存在已匹配記錄
    			System.out.format("Total amount: $%.2f\n", totalAmount);
    		}
    		cur=i;//指向下一條用戶第一條記錄
    	}
    }
}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章