speed/Speed.java

  1 /*
  2   Southeast USA, 2013.  Problem I: Speed Can Cost You
  3   Categories: time (hours, minutes, seconds), speed, distance
  4   Author: Ryan Stansifer
  5  */
  6 import java.io.*;
  7 import java.util.Scanner;
  8 import java.util.*;
  9 
 10 public final class Speed {
 11 
 12    private static final Scanner STDIN =
 13       new Scanner (new BufferedInputStream (System.in));
 14 
 15    private static final PrintStream STDOUT =
 16       new PrintStream (new BufferedOutputStream (System.out));
 17 
 18    public static void main (final String[] args) {
 19 
 20       input: for (int ds=1; /**/; ds++) {
 21          final int d = STDIN.nextInt();   // distance in miles
 22          final int s1 = STDIN.nextInt();  // speed in miles/hours
 23          final int s2 = STDIN.nextInt();  // speed in miles/hours
 24          if (d==0 && s1==0 && s2==0) break;
 25          assert 1<=d && d<=10_000;
 26          assert 1<=s1 && s1<s2 && s2<=1_000;
 27 
 28          /*
 29            Time in seconds is distance divided by speed,
 30            times the number of seconds in an hour.
 31          */
 32 
 33          final double time1 = 3600.0D * d / s1;  // time in seconds 
 34          final double time2 = 3600.0D * d / s2;
 35          final double diff = time1-time2;
 36          // interval in seconds
 37          final long interval = (long) Math.round (time1-time2);
 38          final long hours = interval / 3600L;  // (truncate)
 39          final long minutes = (interval / 60L) % 60L;
 40          final long seconds = interval % 60L;
 41          System.out.printf ("%d:%02d:%02d\n", hours, minutes, seconds);
 42       }
 43       STDOUT.close();
 44    }
 45 }
 46 
 47 /*
 48  * ------------For GNU Emacs ------------
 49  * Local Variables:
 50  * compile-command:  javac Speed.java 
 51  * End:
 52  */