Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.21 KB | None | 0 0
  1. public class Main {
  2. public static void main(String[] args) {
  3. Train train = new Train(5000, 5000);
  4.  
  5. Counter counter = new Counter(train);
  6. counter.countNumberOfCars();
  7. }
  8.  
  9. class Train {
  10. private boolean[] lightsInCar;
  11. private int size;
  12. private int currentCarIndex;
  13. private static Random random = new Random();
  14.  
  15. Train(int minimalSize, int maximumSize) {
  16. size = minimalSize + (int) (Math.random() * (maximumSize - minimalSize + 1));
  17.  
  18. lightsInCar = new boolean[size];
  19.  
  20. for (int i = 0; i < size; i++) {
  21. lightsInCar[i] = random.nextBoolean();
  22. }
  23. currentCarIndex = 0;
  24. }
  25.  
  26. void gotoNextCar() {
  27. currentCarIndex = (currentCarIndex + 1) % size;
  28. }
  29.  
  30. void gotoPreviousCar() {
  31. currentCarIndex = (currentCarIndex + size - 1) % size;
  32. }
  33.  
  34. boolean getLigtsStatusInCar() {
  35. return lightsInCar[currentCarIndex];
  36. }
  37.  
  38. void setLigtsOnInCar(boolean status) {
  39. lightsInCar[currentCarIndex] = status;
  40. }
  41.  
  42. class Counter {
  43. private Train train;
  44. private int minimalNumberOfCars;
  45.  
  46. Counter(Train train) {
  47. this.train = train;
  48. }
  49.  
  50. void countNumberOfCars() {
  51. int guess = guessNumberOfCars();
  52.  
  53. System.out.println("Number of cars where should stop: " + guess);
  54.  
  55. int i = 0;
  56.  
  57. while (true) {
  58.  
  59. i++;
  60.  
  61. if (i == guess) {
  62. train.setLigtsOnInCar(true);
  63.  
  64. break;
  65. } else {
  66. train.setLigtsOnInCar(false);
  67. train.gotoNextCar();
  68. }
  69. }
  70.  
  71. int numberOfCars = 0;
  72.  
  73. while (true) {
  74. train.gotoPreviousCar();
  75.  
  76. numberOfCars++;
  77.  
  78. if (train.getLigtsStatusInCar())
  79. break;
  80.  
  81. if (numberOfCars == guess && !train.getLigtsStatusInCar()) {
  82. minimalNumberOfCars = numberOfCars + 1;
  83. countNumberOfCars();
  84. }
  85. }
  86.  
  87. System.out.println("Number of cars: " + numberOfCars);
  88. }
  89.  
  90. private int guessNumberOfCars() {
  91. // при вызове minimalNumberOfCars всегда 0
  92. int guess = minimalNumberOfCars + (int) (Math.random() * 10000);
  93. return guess;
  94. }
  95.  
  96. public class Main {
  97. public static void main(String[] args) {
  98. Train train = new Train(5000, 5000);
  99.  
  100. Counter counter = new Counter(train);
  101. counter.countNumberOfCars();
  102. }
  103. }
  104.  
  105. class Train {
  106. private boolean[] lightsInCar;
  107. private int size;
  108. private int currentCarIndex;
  109. private static Random random = new Random();
  110.  
  111. Train(int minimalSize, int maximumSize) {
  112. size = minimalSize + (int) (Math.random() * (maximumSize - minimalSize + 1));
  113.  
  114. lightsInCar = new boolean[size];
  115.  
  116. for (int i = 0; i < size; i++) {
  117. lightsInCar[i] = random.nextBoolean();
  118. }
  119. currentCarIndex = 0;
  120. }
  121.  
  122. void gotoNextCar() {
  123. currentCarIndex = (currentCarIndex + 1) % size;
  124. }
  125.  
  126. void gotoPreviousCar() {
  127. currentCarIndex = (currentCarIndex + size - 1) % size;
  128. }
  129.  
  130. boolean getLigtsStatusInCar() {
  131. return lightsInCar[currentCarIndex];
  132. }
  133.  
  134. void setLigtsOnInCar(boolean status) {
  135. lightsInCar[currentCarIndex] = status;
  136. }
  137. }
  138.  
  139. class Counter {
  140. private Train train;
  141. private int minimalNumberOfCars;
  142.  
  143. Counter(Train train) {
  144. this.train = train;
  145. }
  146.  
  147. void countNumberOfCars() {
  148. int guess = guessNumberOfCars();
  149.  
  150. System.out.println("Number of cars where should stop: " + guess);
  151.  
  152. int i = 0;
  153.  
  154. while (true) {
  155.  
  156. i++;
  157.  
  158. if (i == guess) {
  159. train.setLigtsOnInCar(true);
  160.  
  161. break;
  162. } else {
  163. train.setLigtsOnInCar(false);
  164. train.gotoNextCar();
  165. }
  166. }
  167.  
  168. int numberOfCars = 0;
  169.  
  170. while (true) {
  171. train.gotoPreviousCar();
  172.  
  173. numberOfCars++;
  174.  
  175. if (train.getLigtsStatusInCar())
  176. break;
  177.  
  178. if (numberOfCars == guess && !train.getLigtsStatusInCar()) {
  179. minimalNumberOfCars = numberOfCars + 1;
  180. countNumberOfCars();
  181. }
  182. }
  183.  
  184. System.out.println("Number of cars: " + numberOfCars);
  185. }
  186.  
  187. private int guessNumberOfCars() {
  188. // при вызове minimalNumberOfCars всегда 0
  189. int guess = minimalNumberOfCars + (int) (Math.random() * 10000);
  190. return guess;
  191. }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement