WOJ1187-Difference Minimizing

来源:互联网 发布:c语言中用户标识符 编辑:程序博客网 时间:2024/06/05 22:47

Ginger's supervisor Mr. Zhang has great interest in arithmetic; today, Mr. Zhang left her a problem, the last problem of IPRAI's 2nd Round GS-Examination. Can you help the poor little girl to find out how to minimize the sum difference of two piles of numbers?
The problem seems so simple: there are K positive integers; her mission is to divide them into two piles, so that the sums of two piles are the closest.

输入格式

There are several test cases. Each test case contains two lines.
The first line contains an integer K (1 < K < 100) .
The second line contains K integers, X (1 < X < 100) .

输出格式

For each test case, print the minimum difference in one line.

样例输入

35 1 2618 5 5 5 5 2

样例输出

20

裸的01背包

#include<stdio.h>#include<stdlib.h>#include<string.h>int n,sum,target,*dp,data[100];int main(){    int i,j;    while(~scanf("%d",&n)){        sum=0;        for(i=0;i<n;i++){            scanf("%d",&data[i]);            sum+=data[i];        }        target=sum/2;        dp=(int*)calloc(sum+1,4);        dp[0]=1;        for(i=0;i<n;i++)            for(j=target;j>=data[i];j--)                if(dp[j-data[i]])                    dp[j]=1;        for(i=target;i>=0;i--)            if(dp[i])                break;        printf("%d\n",sum-2*i);    }    return 0;}


原创粉丝点击