true和false的01背包,最大容量为总量/2 总数为奇数直接输出false结束 import java.io.*; import java.util.*; public class Main { public static void main(String[] args) throws IOException { int n = nextInt(); int[] list = new int[n]; int sum = 0; for (int i = 0; i < list.length; i++) { list[i] = nextInt(); sum += list[i]; } if(sum%2==1) { System.out.println(false); return; } boolean[] dp = new boolean[sum / 2+1]; dp[0] = true; for (int i = 0; i < n; i++) { for (int j = sum/2; j >= list[i]; j--) { if(dp[j-list[i]]) dp[j] = true; } } System.out.println(dp[sum/2]); } static PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out)); static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); static StreamTokenizer st = new StreamTokenizer(br); static int nextInt() throws IOException { st.nextToken(); return (int) st.nval; } static long nextLong() throws IOException { st.nextToken(); return (long) st.nval; } }