Guest User

Untitled

a guest
Jun 24th, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.17 KB | None | 0 0
  1. package EkstraSkole;
  2.  
  3. import java.util.Random;
  4.  
  5. public class PremieOppgaveFebruar2012 {
  6.     public static void main(String[] args) {
  7.         // Initialize the random and the array
  8.         Random rnd = new Random();
  9.         int[] numbers = new int[60];
  10.  
  11.         // An int-array is automatically filled with 0's, so just fill in the
  12.         // 1's I need
  13.         for (int i = 20; i < 40; i++) {
  14.             numbers[i] = 1;
  15.         }
  16.  
  17.         // Infinite loop
  18.         for (;;) {
  19.             // Initialize the counters
  20.             int firstThird = 0, secondThird = 0, thirdThird = 0;
  21.  
  22.             // Count all the thirds
  23.             for (int i = 0; i < 20; i++) {
  24.                 firstThird += numbers[i];
  25.             }
  26.  
  27.             for (int i = 20; i < 40; i++) {
  28.                 secondThird += numbers[i];
  29.             }
  30.  
  31.             for (int i = 40; i < 60; i++) {
  32.                 thirdThird += numbers[i];
  33.             }
  34.  
  35.             // Print out the bits
  36.             for (int n: numbers) {
  37.                 System.out.print(n);
  38.             }
  39.  
  40.             // Print out the count
  41.             System.out.println(" " + firstThird + " " + secondThird + " "
  42.                     + thirdThird);
  43.  
  44.             // Where in the array will we move a number from
  45.             int current = rnd.nextInt(60);
  46.  
  47.             // Move the number back (false) or forward (true)
  48.             boolean backForth = rnd.nextBoolean();
  49.  
  50.             // Most of the numbers are hard-coded, as it's faster tan using
  51.             // variables
  52.             // The continue's in the nested if's are to restart the loop.
  53.             // Otherwise it might throw an ArrayOutOfBounds (can't be bothered
  54.             // by try/catch)
  55.             if (numbers[current] == 1) {
  56.                 if (current == 59) {
  57.                     if (numbers[0] == 0 && backForth) {
  58.                         numbers[0] = 1;
  59.                         numbers[59] = 0;
  60.  
  61.                         continue;
  62.                     } else if (numbers[58] == 0 && !backForth) {
  63.                         numbers[58] = 1;
  64.                         numbers[59] = 0;
  65.  
  66.                         continue;
  67.                     }
  68.                 } else if (current == 0) {
  69.                     if (numbers[59] == 0 && !backForth) {
  70.                         numbers[59] = 1;
  71.                         numbers[0] = 0;
  72.  
  73.                         continue;
  74.                     } else if (numbers[1] == 0 && backForth) {
  75.                         numbers[1] = 1;
  76.                         numbers[0] = 0;
  77.  
  78.                         continue;
  79.                     }
  80.                 } else if (numbers[current + 1] == 0 && backForth) {
  81.                     numbers[current] = 0;
  82.                     numbers[current + 1] = 1;
  83.                 } else if (numbers[current - 1] == 0 && !backForth) {
  84.                     numbers[current] = 0;
  85.                     numbers[current - 1] = 1;
  86.                 }
  87.             }
  88.         }
  89.     }
  90. }
Add Comment
Please, Sign In to add comment