算法1
对于天平来说,可以称出J 那么-J 也可以(反过来) 所以 f[i][j] = f[i][-j] = f[i][Math.abs(j)]
JAVA 代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = in.nextInt();
int m = 0;
for (int i = 0; i < a.length; i++) {
m += a[i];
}
work(a, n, m);
}
public static void work(int[] a, int n, int m) {
boolean[][] f = new boolean[n][m + 1];
f[0][0] = true;
f[0][a[0]] = true;
for (int i = 1; i < n; i++) {
for (int j = 0; j <= m; j++) {
f[i][j] |= f[i - 1][j];
f[i][j] |= f[i - 1][Math.abs(j - a[i])];
if (j + a[i] <= m) f[i][j] |= f[i - 1][j + a[i]];
}
}
int ans = 0;
for (int i = 1; i <= m; i++) {
if (f[n - 1][i]) ans++;
}
System.out.println(ans);
}
}