# uva 562 Dividing coins

Dividing coins

It‘s commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nickel, which was made of copper. They were both so eager to get it and the fighting was so fierce, they stretched the coin to great length and thus created
copper-wire.

Not commonly known is that the fighting started, after the two Dutch tried to divide a bag with coins between the two of them. The contents of the bag appeared not to be equally divisible. The Dutch of the past couldn‘t stand the fact that a division should
favour one of them and they always wanted a fair share to the very last cent. Nowadays fighting over a single cent will not be seen anymore, but being capable of making an equal division as fair as possible is something that will remain important forever...

That‘s what this whole problem is about. Not everyone is capable of seeing instantly what‘s the most fair division of a bag of coins between two persons. Your help is asked to solve this problem.

Given a bag with a maximum of 100 coins, determine the most fair division between two persons. This means that the difference between the amount each person obtains should be minimised. The value of a coin varies from 1 cent to 500 cents. It‘s not allowed to
split a single coin.

## Input

A line with the number of problems n, followed by n times:

• a line with a non negative integer m ( ) indicating the number of coins in the bag
• a line with m numbers separated by one space, each number indicates the value of a coin.

## Output

The output consists of n lines. Each line contains the minimal positive difference between the amount the two persons obtain when they divide the coins from the corresponding bag.

```2
3
2 3 5
4
1 2 4 6
```

## Sample Output

```0
1
```

Miguel A. Revilla

1998-03-10

AC代码：

```#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <stdlib.h>

using namespace std;

int v;
int dp;

int main(){
int t;
scanf("%d",&t);
while(t--){
int m;
scanf("%d",&m);
int sum=0;
memset(v,0,sizeof(v));
for(int i=1;i<=m;i++){
scanf("%d",&v[i]);
sum+=v[i];
//printf("%d\n",v[i]);
}
sort(v,v+m);
//for(int i=0;i<m;i++){
//    printf("%d\n",&v[i]);
//}
int n;
n=sum/2;
memset(dp,0,sizeof(dp));
for(int i=1;i<=m;i++){
for(int j=0;j<=n;j++){
dp[i][j]=dp[i-1][j];
if(j-v[i]>=0)
dp[i][j]=max(dp[i][j],dp[i-1][j-v[i]]+v[i]);
}
}
int a=dp[m][n];
int b=sum-dp[m][n];
int ans=abs(a-b);
printf("%d\n",ans);
}
return 0;
}```

﻿﻿

uva 562 Dividing coins,布布扣,bubuko.com

## UVA 562 Dividing coins --01背包的变形 01背包的变形. 先算出硬币面值的总和,然后此题变成求背包容量为V=sum/2时,能装的最多的硬币,然后将剩余的面值和它相减取一个绝对值就是最小的差值. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; #define N 50007 int c,d

## UVA 562 Dividing coins （01背包基础）

[题目链接]:click here~~ 代码: /* * Problem: UVA No.562 * Running time: 0MS * Complier: C++ * Author: ACM_herongwei * Create Time: 11:12 2015/9/9 星期三 * zeroonebags * 将金币总价值的一半作为背包容量,然后zeronebags */ #include <stdio.h> #include <iostream> #include <

## UVA 562 Dividing coins (01背包)

//平分硬币问题 //对sum/2进行01背包,sum-2*dp[sum/2] #include <iostream> #include <cstring> #include <algorithm> using namespace std; int value,dp; int main() { int n,m,sum,sum1; cin>>n; while(n--) { cin>>m; sum=0; for(int

## UVA 562 Dividing coins 分硬币（01背包，简单变形） ## UVALive5583 UVA562 Dividing coins

Regionals 1996 >> Europe - Northwestern 问题链接:UVALive5583 UVA562 Dividing coins. 题意简述:输入测试用例数n,每个测试用例包括金币的数量m和m个正整数是金币的币值.将这些金币分为两堆,使得其差值最小,求最小的差值. 问题分析:可以用0/1背包的动态规划方法来解决这个问题.将背包的容量设为所有金币币值之和的一半,将这一堆金币尽可能多地放入背包即可.再计算一下差值即可. 0/1背包动态规划法的递归式为f(j,X),其中j

## Dividing coins （01背包）

It's commonly known that the Dutch have invented copper-wire. Two Dutch men were fighting over a nickel, which was made of copper. They were both so eager to get it and the fighting was so fierce, they stretched the coin to great length and thus crea