Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.07 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. public class BusyJailor {
  4.     private boolean cellsLocked[];
  5.  
  6.     public BusyJailor() {
  7.         int cellCount = 1000;
  8.         int counter = 0;
  9.         cellsLocked = new boolean[cellCount];
  10.         // all cells are set to false (closed)
  11.         Arrays.fill(cellsLocked, false);
  12.  
  13.         // locks / unlocks cells based on pattern
  14.         for (int i = 0; i < cellCount; i++) {
  15.             for (int j = i; j < cellCount; j = j + i + 1) {
  16.                 // if true (open), set to false (closed)
  17.                 if (cellsLocked[j]) {
  18.                     cellsLocked[j] = false;
  19.  
  20.                 // otherwise, it must be false, so set it to true (open)
  21.                 } else {
  22.                     cellsLocked[j] = true;
  23.                 }
  24.             }
  25.         }
  26.  
  27.         // prints out any of the cells that are open
  28.         System.out.println("The free prisoners are:");
  29.         for (int i = 0; i < cellCount; i++) {
  30.             if (cellsLocked[i]) {
  31.                 System.out.print(EasyFormat.format((i + 1), 5));
  32.                 counter++;
  33.             }
  34.  
  35.             // just so that I can print out 5 per line
  36.             if (counter == 5) {
  37.                 System.out.println();
  38.                 counter = 0;
  39.             }
  40.         }
  41.     }
  42.  
  43.     public static void main(String[] args) {
  44.         new BusyJailor();
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement