标签:style blog class code java tar
2 5 2 6 5 8 9 3 2 1 5
0 2
import java.util.Scanner; public class Main{//DP-背包 public static void main(String[] args){ Scanner input=new Scanner(System.in); int N=input.nextInt(); while(N-->0){ int n=input.nextInt(); int a[]=new int[n]; int sum=0; for(int i=0;i<n;i++){ a[i]=input.nextInt(); sum+=a[i]; } int dp[]=new int[sum/2+10]; for(int j=0;j<n;j++) for(int i=sum/2;i>=a[j];i--){ dp[i]=Math.max(dp[i-a[j]]+a[j], dp[i]); } System.out.println(Math.abs(sum-dp[sum/2]*2)); } } } /*import java.util.Scanner; public class Main{//超时 private static int min=Integer.MAX_VALUE; public static void main(String[] args){ Scanner input=new Scanner(System.in); int N=input.nextInt(); while(N-->0){ int n=input.nextInt(); int a[]=new int[n]; min=Integer.MAX_VALUE; for(int i=0;i<n;i++){ a[i]=input.nextInt(); } dfs(a,n-1,0,0); System.out.println(min); } } private static void dfs(int[] a, int n, int x, int y) { if(n<0){ if(Math.abs(x-y)<min) min=Math.abs(x-y); return ; } dfs(a,n-1,x+a[n],y); dfs(a,n-1,x,y+a[n]); } } */
标签:style blog class code java tar
原文地址:http://blog.csdn.net/zhangyiacm/article/details/24923823