TwentyQuestions.java
public class TwentyQuestions
{
public static int search(int lo, int hi)
{
if ((hi - lo) == 1) return lo;
int mid = lo + (hi - lo) / 2;
StdOut.printf("Is it less than %d?
", mid);
if (StdIn.readBoolean()) return search(lo, mid);
else
return
search(mid, hi);
}
public static void main(String[] args)
{
int n = Integer.parseInt(args[0]);
int N = (int) Math.pow(2, n);
StdOut.printf("Think of an integer between %d and %d\n", 0,
N-1);
int v = search(0, N);
System.out.printf("Your number is %d\n", v);
}
}
No comments:
Post a Comment