Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- public class BusyJailor {
- private boolean cellsLocked[];
- public BusyJailor() {
- int cellCount = 1000;
- int counter = 0;
- cellsLocked = new boolean[cellCount];
- // all cells are set to false (closed)
- Arrays.fill(cellsLocked, false);
- // locks / unlocks cells based on pattern
- for (int i = 0; i < cellCount; i++) {
- for (int j = i; j < cellCount; j = j + i + 1) {
- // if true (open), set to false (closed)
- if (cellsLocked[j]) {
- cellsLocked[j] = false;
- // otherwise, it must be false, so set it to true (open)
- } else {
- cellsLocked[j] = true;
- }
- }
- }
- // prints out any of the cells that are open
- System.out.println("The free prisoners are:");
- for (int i = 0; i < cellCount; i++) {
- if (cellsLocked[i]) {
- System.out.print(EasyFormat.format((i + 1), 5));
- counter++;
- }
- // just so that I can print out 5 per line
- if (counter == 5) {
- System.out.println();
- counter = 0;
- }
- }
- }
- public static void main(String[] args) {
- new BusyJailor();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement