Skip to content

Instantly share code, notes, and snippets.

@desmondrawls
Created April 8, 2013 23:23
Show Gist options
  • Save desmondrawls/5341474 to your computer and use it in GitHub Desktop.
Save desmondrawls/5341474 to your computer and use it in GitHub Desktop.
## Cats in Hats You have 100 cats (You are quite lucky to own so many cats!). You have arranged all your cats in a line. Initially, none of your cats have any hats. You take 100 rounds walking around the cats, always starting with the first cat. Every time you stop at a cat, you put a hat on it if it doesn't have one, and you take its hat off if…
import acm.program.*;
public class CatsInHats extends ConsoleProgram {
private boolean changeHat(boolean b){ //reverses the hat-having state of any cat that changeHat gets called on
if (b==true) {
return false};
else return true;
}
boolean[] cats = new boolean[100];
private void printArray(arr){ //prints the index (adjusted by one) of every cat with hat true state
for (int i=0; i<arr.size(); i++){
if arr[i] println(i+1);
else break;
};
private void selectCat(arr){
for (int j=0; j<arr.size(); j++){ //indicates which round
for (int i=0; i<arr.size(); i++){ //indicates which cat
if j==0 { //sets all the cats to no hat-having state
arr.get(i) = false;
break;
}
elseif i%j==0 {
arr.set(i, arr.get(i).changeHat);
}
else break;
};
};
public void run(){
cats.selectCat.printArray;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment