public class BallWeigh {
public static void main (String[] args) {
for (int i = 2; i <= 1000; i++) {
int total = 0, lowest = Integer.MAX_VALUE, highest = -1;
for (int j = 0; j < i; j++) {
Ball[] balls = new Ball[i];
for (int k = 0; k < i; k++) {
balls[k] = new Ball(k == j ? 2 : 1);
}
int n = weigh(balls, 0);
total += n;
if (n < lowest)
lowest = n;
if (n > highest)
highest = n;
}
System.out.println(i + " " + (((float) total) / i) +
" " + lowest +
" " + highest);
}
}
public static int weigh (Ball[] balls, int count) {
if (balls.length == 1)
return count;
int n = balls.length / 3;
if (balls.length % 3 != 0)
n++;
Ball[] scale1 = new Ball[n];
System.arraycopy(balls, 0, scale1, 0, n);
Ball[] scale2 = new Ball[n];
System.arraycopy(balls, n, scale2, 0, n);
Ball[] rest = new Ball[balls.length - 2 * n];
if (rest.length > 0)
System.arraycopy(balls, 2 * n, rest, 0, balls.length - 2 * n);
int s1 = getWeight(scale1), s2 = getWeight(scale2);
if (s1 == s2)
balls = rest;
else if (s1 > s2)
balls = scale1;
else
balls = scale2;
return weigh(balls, ++count);
}
private static int getWeight (Ball[] balls) {
int weight = 0;
for (Ball b : balls)
weight += b.getWeight();
return weight;
}
static class Ball {
private int weight;
public Ball (int weight) {
this.weight = weight;
}
public int getWeight () {
return weight;
}
}
}