March 5, 2005

Program / Task Name:




 Novice / Advanced

Point Value:






Data File(s):

C programmers: your program name must be: prob08.exe
JAVA programmers: your program name must be: prob08.class


Task Description

The simple algorithm by which a single elevator can decide where to stop is:

  • Continue traveling in the same direction while there are remaining requests in that same direction.

  • If there are no further requests in that direction, then stop and become idle, or change direction if there are requests in the opposite direction.

Your task is to implement the basic elevator algorithm as outlined above.  At each floor of our 20-story building, there is a single button to call the elevator to that floor.  You will be given a series of numbers that correspond to the floor to which the elevator has been requested.  A zero (0) means that no request was issued at that time.  Assume that each request or ‘no-op’ (line of input) comes exactly 5 seconds apart and that it also takes the elevator 5 seconds to move between each floor.

Ignore the time it takes to service the request or change direction; in other words, the elevator never stops moving unless there are no pending requests.  If a request is made for a floor at the same time the elevator arrives to that floor, the request will be serviced at that time.  The elevator will service all pending requests to a floor at once.

For this assignment, the elevator will always begin on the 1st floor.  Your program should read input from a file named  Each line of input will be a single integer between 0 and 20 inclusive.  The first request arrives at time=0.  There is no specified termination string in the input file.  In other words your program must detect the end of the file.  Your program should output the number of floors traveled on the first line and the longest time it took to complete a single request on the second.

Example Input

Example Output








In this example, the first request for floor 3 starts the elevator moving.  The next request, for floor 1, comes at time=5s; however the elevator is already at the 2nd floor moving up.  The request will have to wait until the elevator changes direction.  At 10sec, the elevator reaches the 3rd floor to service the first request, and at the same time a request is made for the 2nd floor.  Since there are no more request for floors above three, the elevator will now switch directions and service the requests for the 2nd and 1st floor on its way down.  The elevator traveled 4 floors and took 5 seconds to service request for floor 2, 10 seconds for floor 3, but took 15 seconds to service the request for the 1st floor.


Program Input











Program Output





  (c) 2005 Hewlett-Packard Company