UVa12293 - Box Game(博弈)

There are two identical boxes. One of them contains n balls, while the other box contains one ball. Alice and Bob invented a game with the boxes and balls, which is played as follows:

Alice and Bob moves alternatively, Alice moves first. For each move, the player finds out the box having fewer number of balls inside, and empties that box (the balls inside will be removed forever), and redistribute the balls in the other box. After the redistribution, each box should contain at least one ball. If a player cannot perform a valid move, he loses. A typical game is shown below:

When both boxes contain only one ball, Bob cannot do anything more, so Alice wins.


Question: if Alice and Bob are both clever enough, who will win? Suppose both of them are very smart and always follows a perfect strategy.

Input 

There will be at most 300 test cases. Each test case contains an integer  n  (   2n109 ) in a single line. The input terminates by  n = 0 .

Output 

For each test case, print a single line, the name of the winner.

Sample Input 

2
3
4
0

Sample Output 

Alice
Bob
Alice

import java.io.FileInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.io.PrintWriter;
import java.io.OutputStreamWriter;

public class Main implements Runnable{
	private static final boolean DEBUG = false;
	private BufferedReader cin;
	private PrintWriter cout;
	private StreamTokenizer tokenizer;
	private int n;
	
	private void init() 
	{
		try {
			if (DEBUG) {
				cin = new BufferedReader(new InputStreamReader(
						new FileInputStream("e:\\uva_in.txt")));
			} else {
				cin = new BufferedReader(new InputStreamReader(System.in));
			}

			tokenizer = new StreamTokenizer(cin);
			cout = new PrintWriter(new OutputStreamWriter(System.out));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String next() 
	{
		try {
			 tokenizer.nextToken(); 
			 if (tokenizer.ttype == StreamTokenizer.TT_EOF) return null; 
			 else if (tokenizer.ttype == StreamTokenizer.TT_NUMBER) { 
			 	return String.valueOf((int)tokenizer.nval); 
			} else return tokenizer.sval;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	private boolean input() 
	{
		n = Integer.parseInt(next());
		if (n == 0) return false;

		return true;
	}

	
	
	private void solve() 
	{	
		n++;
		if ((n & (n - 1)) != 0) cout.println("Alice");
		else cout.println("Bob");

		cout.flush();
	}

	public void run()
	{
		init();

		while (input()) {
			solve();
		}
	}
	
	public static void main(String[] args) 
	{
		new Thread(new Main()).start();
	}
}




你可能感兴趣的:(博弈,#,uva)