题意:
(1)排序题,但是细节很复杂。对于ID,按照字母表排序;而每个ID中的所有通话,按照日期从小到大排。
(2)用TreeMap<String, ArrayList<Record>>来存储,一个ID对应一个列表,列表里存储ID的所有通话记录并排好序。而TreeMap里面可以自己写一个Comparator按ID排序
(3)同一个ID的排序完毕的列表里,相邻的on-line和off-line记为一次通话记录。
(4)对于每次通话,封装成一个Record对象,除了存储id,month,day,hour,minute,status外,还可以存储相对于本月1号00:00的相对时间,以及相对于1号00:00的相对话费,这样两次通话记录相对时间相减,就得到打电话的总minute,将相对话费相减,便得到此次通话的话费。
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.TreeMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Collections;
import java.util.Set;
import java.util.Map.Entry;
public class Main {
static int totalFee = 0;
static int fee[] = new int[24];
public static void main(String[] args) throws IOException{
BufferedReader rd = new BufferedReader(new InputStreamReader(System.in));
String fee1[] = rd.readLine().split(" ");
for(int i=0; i<fee.length; i++) {
fee[i] = Integer.parseInt(fee1[i]);
totalFee += fee[i];
}
int n = Integer.parseInt(rd.readLine());
Map<String, ArrayList<Record>> map = new TreeMap<>(new Comparator<String>(){
@Override
public int compare(String o1, String o2) {
return o1.compareTo(o2);
}
});
for(int i=0; i<n; i++) {
String str[] = rd.readLine().split(" ");
Record rec = new Record(str[0], str[1], str[2]);
if(!map.containsKey(str[0]))
map.put(str[0], new ArrayList<Record>());
map.get(str[0]).add(rec);
}
Set<Entry<String, ArrayList<Record>>> entrySet = map.entrySet();
for(Entry<String, ArrayList<Record>> entry: entrySet) {
ArrayList<Record> list = entry.getValue();
Collections.sort(list);
double singleAmount = 0;
double totalAmount = 0;
Record lastRecord = list.get(0);
String output = String.format("%s %02d\n", entry.getKey(), lastRecord.month);
for(int i=0; i<list.size(); i++) {
Record currentRecord = list.get(i);
if(currentRecord.status.equals("on-line")) {
lastRecord = currentRecord;
continue;
}
else {
if(lastRecord.status.equals("on-line")) {
int minute = currentRecord.relativeTime - lastRecord.relativeTime;
singleAmount = (currentRecord.relativeFee - lastRecord.relativeFee)/100.0;
totalAmount += singleAmount;
output += String.format("%s %s %d $%.2f\n",
lastRecord.time.substring(3), currentRecord.time.substring(3), minute, singleAmount);
}
}
lastRecord = currentRecord;
}
output += String.format("Total amount: $%.2f", totalAmount);
if(totalAmount>0)
System.out.println(output);
}
}
static class Record implements Comparable<Record>{
String id;
String time;
int month;
int day;
int hour;
int minute;
String status;
double relativeFee;
int relativeTime;
public Record(String id, String time, String status) {
this.id = id;
this.time = time;
String str[] = time.split(":");
this.month = Integer.parseInt(str[0]);
this.day = Integer.parseInt(str[1]);
this.hour = Integer.parseInt(str[2]);
this.minute = Integer.parseInt(str[3]);
this.status = status;
this.relativeTime = day*1440+hour*60+minute;
this.relativeFee += minute*fee[hour];
for(int i=0; i<hour; i++)
this.relativeFee += fee[i]*60;
this.relativeFee += day*60*totalFee;
}
@Override
public int compareTo(Record r1) {
return this.relativeTime - r1.relativeTime;
}
}
}
写在后面:
JAVA速度一如既往的慢。。。。。。改日再写C的代码。=.=