Description today is the lunar calendar of the beginning of July, five, ACM team ZB's birthday. ZB is and C small plus, never in Wuhan training. He wanted to buy something for the two brothers to celebrate the birthday, after investigation, ZB found that C small plus and never are very fond of watermelon, and a eat is a bunch of the kind, ZB immediately determined to buy a bunch of watermelon. When he was ready to give the watermelon to C small plus and never, met a problem, never and C small plus not live, can only divide the watermelon into two piles to them, in order to everyone is fair, he wants to let two piles of weight difference is the smallest. The weight of each watermelon is known, can you help him? Enter multiple sets of test data (<=1500). Data at the end of the first line of EOF input watermelon number n (1≤n≤20) The second line has n number, W1, ..., Wn (1≤wi≤10000) respectively represents the weight of each watermelon output output divided into two piles of quality difference sample input 55 8 13 27 14 Sample Output 3//This is part and The problem is to compare all the occurrences and statistics, comparing these with the 22 difference. thereby outputting the minimum value. #include <stdio.h> #include <math.h> #include <algorithm>using namespace Std;int a[21],total,n,m; void Search (int s,int i) {if (i==n) return; if (Fabs (2*s-total) < m)//part of and is S, then the other part of the and is total-s, while S-(total-s) = = 2*s-total m = fabs (2*s-total); Update minimum value search (s+a[i],i+1); Search (s,i+1); return;} int main () {int i,j; while (~SCANF ("%d", &n)) {total = 0,m = 999999; for (i = 0;i<n;i++) {scanf ("%d", &a[i]); Total + = A[i]; } search (0,0); printf ("%d\n", m); } return 0;}
ZB's Birthday