老哥们看看错在哪里了,只过了63%(代码写的丑请见谅)
import java.util.Scanner; public class Main { public static double res = Double.MIN_VALUE; public static String str = ""; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int x = scanner.nextInt(); int y = scanner.nextInt(); int n = x + y; int[] arr = new int[n]; for (int i = 0; i < n; i++) { arr[i] = scanner.nextInt(); } boolean[] AB = new boolean[n]; dfs(arr, AB, x, y, 0, 0); System.out.println(str); } public static void dfs(int[] arr, boolean[] AB, int x, int y, int a, int b) { if (a + b == x + y) { double avg1 = 0, avg2 = 0; for (int i = 0; i < arr.length; i++) { if (AB[i]) avg1 += arr[i]; else avg2 += arr[i]; } double d1 = avg1 / x; double d2 = avg2 / y; if (d1 + d2 > res) { res = d1 + d2; StringBuilder sb = new StringBuilder(); for (int i = 0; i < AB.length; i++) { if (AB[i]) sb.append('A'); else sb.append('B'); } str = sb.toString(); } return; } if (a < x) { AB[a + b] = true; dfs(arr, AB, x, y, a + 1, b); AB[a + b] = false; } if (b < y) { dfs(arr, AB, x, y, a, b + 1); } } }
全部评论
(1) 回帖