🌕🌑🌑🌑🌑
# 題目連結
- 題目連結
- Online Judge
- uDebug
# 題目說明
Time limit: 3.000 seconds
# 題目
Gondwanaland Telecom makes charges for calls according to distance and time of day. The basis of the charging is contained in the following schedule, where the charging step is related to the distance:
Charging Step (distance) | Day Rate 8am to 6pm | Evening Rate 6pm to 10pm | Night Rate 10pm to 8am |
---|---|---|---|
A | 0.10 | 0.06 | 0.02 |
B | 0.25 | 0.15 | 0.05 |
C | 0.53 | 0.33 | 0.13 |
D | 0.87 | 0.47 | 0.17 |
E | 1.44 | 0.80 | 0.30 |
All charges are in dollars per minute of the call. Calls which straddle a rate boundary are charged according to the time spent in each section. Thus a call starting at 5:58 pm and terminating at 6:04 pm will be charged for 2 minutes at the day rate and for 4 minutes at the evening rate. Calls less than a minute are not recorded and no call may last more than 24 hours.
Write a program that reads call details and calculates the corresponding charges.
# Input
Input lines will consist of the charging step (upper case letter ‘A’..‘E’), the number called (a string of 7 digits and a hyphen in the approved format) and the start and end times of the call, all separated by exactly one blank. Times are recorded as hours and minutes in the 24 hour clock, separated by one blank and with two digits for each number. Input will be terminated by a line consisting of a single ‘#’.
# Output
Output will consist of the called number, the time in minutes the call spent in each of the charge categories, the charging step and the total cost in the format shown below.
Note: The first line of the Sample Output below in not a part of the output, but only to show the exact tabulation format it must follow.
# Sample Input
A 183-5724 17 58 18 04
#
# Sample Output
10 16 22 28 31 39
183-5724 2 4 0 A 0.44
# 解題技巧
依舊可以暴力解,不過有小技巧可以讓程式更簡潔!
# Solution
import java.util.*; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner sc = new Scanner(System.in); | |
char distance = sc.next().charAt(0); | |
Map<Character, double[]> rate = new HashMap<>(); | |
rate.put('A', new double[] { 0.02, 0.1, 0.06, 0.02, 0.1, 0.06, 0.02 }); | |
rate.put('B', new double[] { 0.05, 0.25, 0.15, 0.05, 0.25, 0.15, 0.05 }); | |
rate.put('C', new double[] { 0.13, 0.53, 0.33, 0.13, 0.53, 0.33, 0.13 }); | |
rate.put('D', new double[] { 0.17, 0.87, 0.47, 0.17, 0.87, 0.47, 0.17 }); | |
rate.put('E', new double[] { 0.3, 1.44, 0.8, 0.3, 1.44, 0.8, 0.3 }); | |
int[] time = new int[] { 480, 1080, 1320, 1920, 2520, 2760, 2880 }; | |
while (distance != '#') { | |
String plane = sc.next(); | |
int startHr = sc.nextInt(); | |
int startMin = sc.nextInt(); | |
int endHr = sc.nextInt(); | |
int endMin = sc.nextInt(); | |
int start = startHr * 60 + startMin; | |
int end = endHr * 60 + endMin; | |
if (start >= end) { | |
end += 24 * 60; | |
} | |
int[] ansTime = new int[7]; | |
double cost = 0; | |
for (int i = 0; i < 7; i++) { | |
if (start <= time[i]) { | |
if (end <= time[i]) { | |
ansTime[i] = end - start; | |
cost += rate.get(distance)[i] * ansTime[i]; | |
break; | |
} else { | |
ansTime[i] = time[i] - start; | |
start = time[i]; | |
cost += rate.get(distance)[i] * ansTime[i]; | |
} | |
} | |
} | |
System.out.printf("%10s%6d%6d%6d%3s%8.2f\n", plane, ansTime[1] + ansTime[4], ansTime[2] + ansTime[5], | |
ansTime[0] + ansTime[3] + ansTime[6], distance, cost); | |
distance = sc.next().charAt(0); | |
} | |
} | |
} |
單字
** **
!! !!
片語 & 搭配詞
!! !!