speed/speed_vanb.java

  1 
  2 import java.io.*;
  3 import java.util.*;
  4 import java.awt.geom.*;
  5 
  6 /**
  7  * Solution to Speed Kills
  8  * 
  9  * @author vanb
 10  */
 11 public class speed_vanb
 12 {
 13     public Scanner sc;
 14     public PrintStream ps;
 15     
 16     /**
 17      * Driver.
 18      * @throws Exception
 19      */
 20     public void doit() throws Exception
 21     {
 22         sc = new Scanner( new File( "speed.judge" ) );
 23         ps = new PrintStream( new FileOutputStream( "speed.solution" ) );
 24         
 25         for(;;)
 26         {
 27             int distance = sc.nextInt();
 28             if( distance==0 ) break;
 29             
 30             double s1 = sc.nextDouble();
 31             double s2 = sc.nextDouble();
 32             
 33             // Time, in seconds, to travel the given distance at the given speed.
 34             // distance/s1 and distance/s2 give the times in hours, since speed is in MPH
 35             // So, we've got to multiply by 60*60 to convert hours to seconds
 36             double t1 = distance * 60.0 * 60.0 / s1;
 37             double t2 = distance * 60.0 * 60.0 / s2;
 38             
 39             // Difference in seconds, rounded
 40             int dt = (int)Math.round( t1-t2 );
 41             
 42             // Break out hours, minutes, seconds
 43             int seconds = dt % 60;
 44             int minutes = (dt / 60) % 60;
 45             int hours = dt / 60 / 60;
 46             
 47             ps.printf( "%d:%02d:%02d", hours, minutes, seconds );
 48             ps.println();
 49         }
 50     }
 51     
 52     /**
 53      * @param args
 54      */
 55     public static void main( String[] args ) throws Exception
 56     {
 57         new speed_vanb().doit();
 58         
 59 //        Random r = new Random();
 60 //        for( int i=0; i<100; i++ )
 61 //        {
 62 //            int d = r.nextInt(10000)+1;
 63 //            for(;;)
 64 //            {
 65 //                int s1 = r.nextInt(1000)+1;
 66 //                int s2 = r.nextInt(1000)+1;
 67 //                if( s1!=s2 ) 
 68 //                {
 69 //                    if( s1<s2 ) System.out.println( d +     + s1 +     + s2 );
 70 //                    else System.out.println( d +     + s2 +     + s1 );
 71 //                    break;
 72 //                }
 73 //            }
 74 //        }
 75     }   
 76 }