JAVA zb的生日

import java.util.Scanner;

public class Main{
		static int[] w = new int[20];
		static int n=0, result=10000;
		public static void main(String[] args) {
			Scanner scan = new Scanner(System.in);
			while(scan.hasNext()) {
				result=10000;		// 重置result,做题时忘记重置了,导致一直错
				n = scan.nextInt();
				for(int i=0; i<n; i++) {
					w[i] = scan.nextInt();
				}
				dfszb(0, 0,0);
				System.out.println(result);
			}
		}
		
		public static void dfszb(int index, int a, int b) {
			if(index == n) {
				int temp = a>b?a-b:b-a;
				if(temp < result) {
					result = temp;
				}
				return;
			} else {
				dfszb(index+1, a+w[index], b);
				dfszb(index+1, a, b+w[index]);
			}
			
		}
}

猜你喜欢

转载自blog.csdn.net/weixin_37378399/article/details/84333682
今日推荐