[Post] 0-1 backpack: banknote combination

Source: Internet
Author: User

There are unlimited number of currency notes of 1, 5, 10, 20, and 50 denominations. How many methods can be used to generate RMB 100?

Stupid method: Brute Force enumeration ~

1 # include <iostream>
2 # include <cstring>
3 using namespace STD;
4
5 const int Len = 5;
6 int ans = 0;
7 int M [Len] = {50, 20, 10, 5, 1 };
8 inttotal [Len] = {2, 5, 10, 20,100 };
9 int N [Len];
10 int sum = 100;
11
12 Void getnum (int m [], int N [])
13 {// Method 1: similar to n-digit K-enumeration, phone number word
14 While (true)
15 {
16 int rel = 0;
17 For (INT I = 0; I <5; I ++)
18 rel + = m [I] * n [I];
19 if (rel = sum)
20 {
21 ans ++;
22 For (INT I = 0; I <Len; I ++)
23 cout <n [I] <"";
24 cout <Endl;
25}
26
27 int K = len-1;
28 while (k> = 0)
29 {
30 if (N [k] <total [k])
31 {
32 N [k] ++;
33 break;
34}
35 else
36 {
37 N [k] = 0;
38 K --;
39}
40}
41 if (k <0)
42 break;
43}
44
45}
46
47 void recursivegetnum (int m [], int N [], int index)
48 {// Method 2: Recursion
49 If (Index = Len)
50 {
51 int rel = 0;
52 for (INT I = 0; I <Len; I ++)
53 rel + = m [I] * n [I];
54 if (rel = sum)
55 {
56 ans ++;
57 for (INT I = 0; I <Len; I ++)
58 cout <n [I] <"";
59 cout <Endl;
60}
61 return;
62}
63 for (N [Index] = 0; n [Index] <= total [Index]; n [Index] ++)
64 {
65 recursivegetnum (m, n, index + 1 );
66}
67
68}
69
70 int main ()
71 {
72 getnum (m, n );
73 // recursivegetnum (m, n, 0 );
74 cout <ans <Endl;
75 return 0;
76}

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.