Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package EkstraSkole;
- import java.util.Random;
- public class PremieOppgaveFebruar2012 {
- public static void main(String[] args) {
- // Initialize the random and the array
- Random rnd = new Random();
- int[] numbers = new int[60];
- // An int-array is automatically filled with 0's, so just fill in the
- // 1's I need
- for (int i = 20; i < 40; i++) {
- numbers[i] = 1;
- }
- // Infinite loop
- for (;;) {
- // Initialize the counters
- int firstThird = 0, secondThird = 0, thirdThird = 0;
- // Count all the thirds
- for (int i = 0; i < 20; i++) {
- firstThird += numbers[i];
- }
- for (int i = 20; i < 40; i++) {
- secondThird += numbers[i];
- }
- for (int i = 40; i < 60; i++) {
- thirdThird += numbers[i];
- }
- // Print out the bits
- for (int n: numbers) {
- System.out.print(n);
- }
- // Print out the count
- System.out.println(" " + firstThird + " " + secondThird + " "
- + thirdThird);
- // Where in the array will we move a number from
- int current = rnd.nextInt(60);
- // Move the number back (false) or forward (true)
- boolean backForth = rnd.nextBoolean();
- // Most of the numbers are hard-coded, as it's faster tan using
- // variables
- // The continue's in the nested if's are to restart the loop.
- // Otherwise it might throw an ArrayOutOfBounds (can't be bothered
- // by try/catch)
- if (numbers[current] == 1) {
- if (current == 59) {
- if (numbers[0] == 0 && backForth) {
- numbers[0] = 1;
- numbers[59] = 0;
- continue;
- } else if (numbers[58] == 0 && !backForth) {
- numbers[58] = 1;
- numbers[59] = 0;
- continue;
- }
- } else if (current == 0) {
- if (numbers[59] == 0 && !backForth) {
- numbers[59] = 1;
- numbers[0] = 0;
- continue;
- } else if (numbers[1] == 0 && backForth) {
- numbers[1] = 1;
- numbers[0] = 0;
- continue;
- }
- } else if (numbers[current + 1] == 0 && backForth) {
- numbers[current] = 0;
- numbers[current + 1] = 1;
- } else if (numbers[current - 1] == 0 && !backForth) {
- numbers[current] = 0;
- numbers[current - 1] = 1;
- }
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment