fork(4) download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Test
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. Balls.printTest();
  10. }
  11.  
  12. public static final class Balls {
  13.  
  14. private static final int A = 10;
  15. private static final int B = 11;
  16.  
  17. private static final int LEFT_BIGGER = 1;
  18. private static final int RIGHT_BIGGER = 2;
  19. private static final int SAME = 4;
  20.  
  21. public static void printTest() {
  22. System.out.println(Balls.test(true));
  23. System.out.println(Balls.test(false));
  24. }
  25.  
  26. public static String test(boolean weight) {
  27. int[] array = new int[12];
  28. StringBuilder builder = new StringBuilder();
  29.  
  30. for (int i = 0; i < array.length; ++i) {
  31. for (int w = 0; w < array.length; ++w) {
  32. array[w] = 5;
  33. }
  34. array[i] += weight ? 2 : -2;
  35.  
  36. Balls balls = new Balls(array);
  37.  
  38. builder.append("pos = 0123456789AB\n");
  39. builder.append("array = ");
  40. for (int w : array) {
  41. builder.append(w);
  42. }
  43. builder.append('\n');
  44. builder.append("res = ");
  45. balls.calculateNumber();
  46. int number = balls.getResultPosition();
  47. for (int w = 0; w < array.length; ++w) {
  48. builder.append(w == number ? '*' : ' ');
  49. }
  50. builder.append(" // number = ");
  51. builder.append(number);
  52. builder.append("; steps = ");
  53. builder.append(balls.getStepsCount());
  54. builder.append('\n');
  55. }
  56. return builder.toString();
  57. }
  58.  
  59. private int[] in;
  60. private int steps;
  61. private int number = -1;
  62.  
  63. public Balls(int[] in) {
  64. this.in = in;
  65. }
  66.  
  67. public void calculateNumber() {
  68. this.steps = 0;
  69.  
  70. int step1 = compare(sum(0, 1, 2, 3), sum(4, 5, 6, 7));
  71.  
  72. switch (step1) {
  73. case SAME: {
  74. int step2 = compare(sum(0, 1), sum(8, 9));
  75. switch (step2) {
  76. case SAME: {
  77. number = compare(in[0], in[B]) == SAME ? A : B;
  78. break;
  79. }
  80. default: {
  81. number = compare(in[0], in[9]) == SAME ? 8 : 9;
  82. break;
  83. }
  84. }
  85. break;
  86. }
  87. case LEFT_BIGGER: {
  88. int step2 = compare(sum(0, 1, 4), sum(2, 3, 5));
  89. switch (step2) {
  90. case LEFT_BIGGER: {
  91. int tempState = compare(in[0], in[1]);
  92. number = tempState == SAME ? 5 : tempState == LEFT_BIGGER ? 0 : 1;
  93. break;
  94. }
  95. case RIGHT_BIGGER: {
  96. int tempState = compare(in[2], in[3]);
  97. number = tempState == SAME ? 4 : tempState == LEFT_BIGGER ? 2 : 3;
  98. break;
  99. }
  100. case SAME: {
  101. number = compare(in[6], in[7]) == LEFT_BIGGER ? 7 : 6;
  102. break;
  103. }
  104. }
  105. break;
  106. }
  107. case RIGHT_BIGGER: {
  108. int step2 = compare(sum(0, 1, 4), sum(2, 3, 5));
  109. switch (step2) {
  110. case LEFT_BIGGER: {
  111. int newState = compare(in[2], in[3]);
  112. number = newState == SAME ? 4 : newState == LEFT_BIGGER ? 3 : 2;
  113. break;
  114. }
  115. case RIGHT_BIGGER: {
  116. int tempState = compare(in[0], in[1]);
  117. number = tempState == SAME ? 5 : tempState == LEFT_BIGGER ? 1 : 0;
  118. break;
  119. }
  120. case SAME: {
  121. number = compare(in[6], in[7]) == LEFT_BIGGER ? 6 : 7;
  122. break;
  123. }
  124. }
  125. break;
  126. }
  127. }
  128. }
  129.  
  130. public int getResultPosition() {
  131. return number;
  132. }
  133.  
  134. public int getStepsCount() {
  135. return steps;
  136. }
  137.  
  138. private int compare(int l, int r) {
  139. ++steps;
  140. return l > r ? LEFT_BIGGER : l < r ? RIGHT_BIGGER : SAME;
  141. }
  142.  
  143. private int sum(int... array) {
  144. int result = 0;
  145. for (int i : array) {
  146. result += in[i];
  147. }
  148. return result;
  149. }
  150. }
  151.  
  152.  
  153. }
Success #stdin #stdout 0.1s 320448KB
stdin
Standard input is empty
stdout
pos   = 0123456789AB
array = 755555555555
res   = *            // number = 0; steps = 3
pos   = 0123456789AB
array = 575555555555
res   =  *           // number = 1; steps = 3
pos   = 0123456789AB
array = 557555555555
res   =   *          // number = 2; steps = 3
pos   = 0123456789AB
array = 555755555555
res   =    *         // number = 3; steps = 3
pos   = 0123456789AB
array = 555575555555
res   =     *        // number = 4; steps = 3
pos   = 0123456789AB
array = 555557555555
res   =      *       // number = 5; steps = 3
pos   = 0123456789AB
array = 555555755555
res   =       *      // number = 6; steps = 3
pos   = 0123456789AB
array = 555555575555
res   =        *     // number = 7; steps = 3
pos   = 0123456789AB
array = 555555557555
res   =         *    // number = 8; steps = 3
pos   = 0123456789AB
array = 555555555755
res   =          *   // number = 9; steps = 3
pos   = 0123456789AB
array = 555555555575
res   =           *  // number = 10; steps = 3
pos   = 0123456789AB
array = 555555555557
res   =            * // number = 11; steps = 3

pos   = 0123456789AB
array = 355555555555
res   = *            // number = 0; steps = 3
pos   = 0123456789AB
array = 535555555555
res   =  *           // number = 1; steps = 3
pos   = 0123456789AB
array = 553555555555
res   =   *          // number = 2; steps = 3
pos   = 0123456789AB
array = 555355555555
res   =    *         // number = 3; steps = 3
pos   = 0123456789AB
array = 555535555555
res   =     *        // number = 4; steps = 3
pos   = 0123456789AB
array = 555553555555
res   =      *       // number = 5; steps = 3
pos   = 0123456789AB
array = 555555355555
res   =       *      // number = 6; steps = 3
pos   = 0123456789AB
array = 555555535555
res   =        *     // number = 7; steps = 3
pos   = 0123456789AB
array = 555555553555
res   =         *    // number = 8; steps = 3
pos   = 0123456789AB
array = 555555555355
res   =          *   // number = 9; steps = 3
pos   = 0123456789AB
array = 555555555535
res   =           *  // number = 10; steps = 3
pos   = 0123456789AB
array = 555555555553
res   =            * // number = 11; steps = 3