/* * BusSchedule.java * * Created on October 6, 2002, 8:32 PM */ import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.StringTokenizer; /** * This class is used to determine when the next bus will be arriving at the * bus station. * * @author Aaron McDowell */ public class BusSchedule { /** An array of Bus objects */ private static Bus[] buses = null; /** The arrival time of the passenger */ private static int arrival; /** The amount of time the passenge has to wait */ private static int waitTime; /** End of input flag */ public static final String END_OF_INPUT = "ENDOFINPUT"; /** Creates a new instance of BusSchedule */ public BusSchedule() { super(); } /** * Calculates how long the passenger has to wait from the array of buses * and their route schedules. */ public static void calculateNextArrival() { // Loop through the buses to determine when each will arrive at the stop for (int i = 0; i < buses.length; i++) { // retrieve the schedule for this bus int[] schedule = buses[i].getSchedule(); int total = 0; int routeNum = 0; // In this loop, we will continue to add up the route durations until // the total is greater than the arrival time. while (total < arrival) { total += schedule[routeNum++]; // if we reach the end of the array, start from the beginning if (routeNum >= schedule.length) { routeNum = 0; } } // Compare the wait time to the short wait time of previous buses if (((total - arrival) < waitTime) || (i == 0)) { waitTime = total - arrival; } } } public static void main(String args[]) { String line = null; try { BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) ); line = in.readLine(); while ( (line != null) && ( !line.trim().equals(END_OF_INPUT) ) ) { StringTokenizer input = new StringTokenizer(line); // create a StringTokenizer input.nextToken(); // throw away 'START' int numberOfBuses = Integer.parseInt(input.nextToken()); // number of buses buses = new Bus[numberOfBuses]; // create an array of buses // now we loop through the buses and read in their schedule for (int i = 0; i < numberOfBuses; i++) { buses[i] = new Bus(); // create a new bus input = new StringTokenizer(in.readLine()); // read the route durations int numberOfRoutes = input.countTokens(); // count the number of routes int routes[] = new int[numberOfRoutes]; for (int j = 0; j < numberOfRoutes; j++) { routes[j] = Integer.parseInt(input.nextToken()); } buses[i].setSchedule(routes); // set the buses schedule } arrival = Integer.parseInt(in.readLine()); // read in the arrival time calculateNextArrival(); // call the method to caluculate the next arrival System.out.println(waitTime); // print the lowest wait time line = in.readLine(); // should be 'END' line = in.readLine(); // see if there is another data set } } catch (Exception e) { e.printStackTrace(); } } } /** * This class encapsulates the data for the schedule of a single bus. */ class Bus { // This int array contains the durations of each route the bus runs // in the order it runs them. private int[] schedule = null; /** * public constructor */ public Bus() { super(); } /** * Sets the bus schedule for this bus *@param int[] An array of route durations */ public void setSchedule(int routes[]) { schedule = routes; } /** * Retrieves the bus schedule for this bus * @return int[] An array of route durations */ public int[] getSchedule() { return schedule; } }