Source code for submission s613

Go to diff to previous submission

Ants.java

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. package acm;
  7.  
  8. import java.util.Scanner;
  9.  
  10. /**
  11.  *
  12.  * @author cteam030
  13.  */
  14. public class Ants {
  15.  
  16. public static void main(String[] args) {
  17.  
  18. Scanner s = new Scanner(System.in);
  19.  
  20. while(s.hasNext())
  21. {
  22. int d = s.nextInt();
  23. int count = s.nextInt();
  24. int max = 0;
  25. int middle1 = -1;
  26. int middle2 = -1;
  27.  
  28. for(int i = 0; i < count; i++)
  29. {
  30. int pos = s.nextInt();
  31. String dir = s.next();
  32.  
  33. int len = dir.equals("R") ? d - pos : pos;
  34.  
  35. if(len > max)
  36. {
  37. max = len;
  38. }
  39.  
  40. double delta = Math.abs(pos - d/2);
  41. if(delta < Math.abs(middle1 - d/2))
  42. {
  43. middle1 = pos;
  44. middle2 = -1;
  45. }else if(delta == Math.abs(middle1 - d/2))
  46. {
  47. middle2 = pos;
  48. }
  49. }
  50.  
  51. System.out.printf("The last ant will fall down in %d seconds - started at %d", max, middle1);
  52.  
  53. if(middle2 >= 0)
  54. {
  55. System.out.printf(" and %d", middle2);
  56. }
  57.  
  58. System.out.printf(".%n");
  59.  
  60. }
  61.  
  62. }
  63.  
  64. }
  65.  

Diff to submission s588

Ants.java

--- c4.s588.cteam030.ants.java.0.Ants.java
+++ c4.s613.cteam030.ants.java.0.Ants.java
@@ -39,14 +39,11 @@
                 
                 double delta = Math.abs(pos - d/2);
-                if(delta < Math.abs(middle2 - d/2))
+                if(delta < Math.abs(middle1 - d/2))
                 {
-                    if(delta < Math.abs(middle1 - d/2))
-                    {
-                        middle2 = middle1;
-                        middle1 = pos;
-                    }else
-                    {
-                        middle2 = pos;
-                    }
+                    middle1 = pos;
+                    middle2 = -1;
+                }else if(delta == Math.abs(middle1 - d/2))
+                {
+                    middle2 = pos;
                 }
             }