CMSC 201
Programming Project One Solutions
General Comments
Solution 1 -- The brute force approach
Given the assumptions
that 0 <= start time < end time <= 2359, there are 10 possible cases
as listed below.
- start time in time-zone 1 and end time in time-zone 1
- start time in time-zone 1 and end time in time-zone 2
- start time in time-zone 1 and end time in time-zone 3
- start time in time-zone 1 and end time in time-zone 4
- start time in time-zone 2 and end time in time-zone 2
- start time in time-zone 2 and end time in time-zone 3
- start time in time-zone 2 and end time in time-zone 4
- start time in time-zone 3 and end time in time-zone 3
- start time in time-zone 3 and end time in time-zone 4
- start time in time-zone 4 and end time in time-zone 4
In solution 1,
the program determines which of the 10 possible
cases exist using a large if/else-if structure. It then calculates
the minutes
for each time-zone contained in the start time/end time range.
Each of these cases is handled in one large if/else-if statment
Solution 2 -- A simpler (but less efficent) way
In solution 2,
the program loops from start time to end time.
It determines to which time-zone each minute of the loop belongs,
and increments the number of minutes for that time-zone.
In this loop, the loop index is the time that the minute begins,
which determines the fee for that minute.
Solution 3 -- a more complex but efficient sulution
In solution 3,
- the program checks if the start time and end time are in time-zone 1.
- If so, simply subtract them to get the answer.
- If not, subtract the start time start from the start time
of time-zone 2 to get the minutes in time-zone 1. Then "move" the
start time to the beginning of time-zone 2
- check if the start time and endtime are both in time-zone 2.
- If so, subtract them to get the minutes in time-zone 2.
- If not, subtract the start time from the start of time-zone 3
to get the minutes in time-zone 2, then "move the start
time to the beginning of time-zone 3.
- Continue this process for all time-zones.