Nyoj 325 ZB's Birthday

Source: Internet
Author: User

ZB's Birthdaytime limit: ms | Memory limit:65535 KB


Describe
today is the lunar calendar July early five, ACM team ZB's birthday. ZB is and C small plus, never in Wuhan training. He wants to buy a celebration for these two brothers.

Day, after investigation, ZB found C small plus and never are like to eat watermelon, and a eat is a bunch of kind, ZB immediately determined to buy a bunch of watermelon. When

He was going to give the watermelon to C 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 be fair to everyone, he wanted to minimize the difference in weight between the two piles. The weight of each watermelon is known, can you help him?
Input

multiple sets of test data (<=1500). Data ends with EOF

First line Input watermelon number N (1≤n≤20)

The second line has n numbers, W1, ..., and Wn (1≤wi≤10000) represents the weight of each watermelon, respectively.
Output

quality difference After the output is divided into two piles

Sample input
55 8 13) 27 14
Sample output
3


each watermelon has two possibilities, either to the C small Plus, or not to C small Plus, when the processing of a watermelon, update the minimum difference, I am using search to do.

#include <stdio.h> #include <math.h> #include <string.h> #include <algorithm>using namespace Std;int a[21],total,n,m;void Search (int s,int i)//s represents the weight of one pair of watermelons, I represents the watermelon {    if (i==n) return that is currently being processed;    if (Fabs (2*s-total) < m)//fabs (2*s-total) d represents the difference between the current two stacks of watermelon        m = fabs (2*s-total);    Search (s+a[i],i+1);//Divide the watermelon into this group of    search (s,i+1);//do not give the watermelon to this group of    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;}



Nyoj 325 ZB's Birthday

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.