Skip to content

Instantly share code, notes, and snippets.

@yokolet
Created May 24, 2017 05:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yokolet/334e8e49afee1a9061ee70c2d540601f to your computer and use it in GitHub Desktop.
Save yokolet/334e8e49afee1a9061ee70c2d540601f to your computer and use it in GitHub Desktop.
public class NimGame {
static String doesFirstWin(int[] piles) {
int n = piles.length;
int x = piles[0];
for (int i = 1; i < n; i++) {
x ^= piles[i];
}
if (x != 0) {
return "First";
} else {
return "Second";
}
}
public static void main(String[] args) {
int[] piles0 = {2, 1, 4};
System.out.println(doesFirstWin(piles0));
int[] piles1 = {1, 1};
System.out.println(doesFirstWin(piles1));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment