HDU 1059 劃分 題解

2021-09-12 14:26:29 字數 1134 閱讀 4638

現有面值為1、2、3、4、5、6的硬幣若干枚,現在需要知道能不能將這些硬幣分成等額的兩堆。

輸入:
每行輸入6個正整數,分別表是面值為1、2、3、4、5、6的硬幣的個數,若輸入6個0代表輸入結束。單種硬幣的數量不會超過20000。

輸出:
若能分割,輸出can be divided.'',若不能輸出can』t be divided.』』

首先將每種金額的硬幣都分成數堆:

比如一元硬幣有n個,那麼用乙個迴圈,把他分成1個一堆,2個一堆,4個一堆…2k個一堆.

最後剩下一些不能按照以上規則堆成一堆.把剩下的也堆成一堆.

這時,凡是這些硬幣能夠組成的金額,都能夠由以上產生的幾堆硬幣組成.

然後就可以轉換成比較簡單的揹包問題.

我們算出所有金幣的總金額sum.

如果sum是單數,那麼不能劃分.

如果這幾堆硬幣能夠組成sum/2,那麼就可以劃分

如果不能.則不能劃分.

#includelong c1, c2, c3, c4, c5, c6, sum, t = 1;

long coin[100], cn, answer[210000];

int part(int a, int b)

if(a)

}int dp()

for(i = 0; i < cn; i++)

if(answer[sum])}}

printf("collection #%d:\n", t++);

printf("can't be divided.\n\n");

}int main()

sum /= 2;

cn = 0;

part(c1, 1);

part(c2, 2);

part(c3, 3);

part(c4, 4);

part(c5, 5);

part(c6, 6);

dp();

scanf("%ld%ld%ld%ld%ld%ld", &c1, &c2, &c3, &c4, &c5, &c6);

}}

HDU 1059 多重揹包

個人覺得,這個可以作為多重揹包的模板使用,其中包括了簡單的0 1揹包和完全揹包,分清楚它們三個的區別。include include define n 60005 define max a,b a b?a b int c n void complete int cost,int weight,int...

HDU 1059 揹包問題

揹包問題 題意 輸入 個整數,分別表示 到 六個數字的個數,問能否組合成兩組,這兩組所有數字的總和相同 include include include include include include include includeusing namespace std const int maxn ...

hdu 1059 多重揹包

題目的意思 有一堆大理石,按其大小分為1 6 種價值,兩個人想分得的價值相同的大理石,已知每種價值的大理石個數,問是否能讓兩個人分得價值相同的大理石?題目輸入 1 0 1 2 0 0 1 0 0 0 1 1 0 0 0 0 0 0 題目輸出 collection 1 can t be divided...