import java.util.Arrays; import java.util.Random; public class DrawingPanel { static int N = 8; public static void main(String[] args) { float balls[] = new float[N]; Arrays.fill(balls, 1f); Random rand = new Random(); balls[rand.nextInt(N) % balls.length] = 1.2f; int rs = compare(balls, 0, balls.length); for(int i = 0 ; i < balls.length ;i++){ System.out.println(String.format("%c : %.1f", 'A'+i, balls[i])); } System.out.println("ans : " + (char)('A'+rs)); } static int compare(float[] balls, int first, int last) { int size = last-first; if(size == 1){ return first ; } int group = size / 3 ; if( size % 3 != 0) group = size / 3 + 1 ; float gp1 = sum(balls, first, first+group); float gp2 = sum(balls, first+group, first+2*group); if(gp1 > gp2){ return compare(balls, first, first+group); }else if(gp1 < gp2){ return compare(balls, first+group, first+2*group); }else{ return compare(balls, first+2*group, last); } } static float sum(float arr[], int first, int last){ float sum = 0f ; for(int i = first ; i < last ; i++) sum += arr[i]; return sum ; } }
想到可以把他貼過來~~
沒有留言:
張貼留言