Money Systems
The cows have not only created their own government but they have chosen to create their own money system. in their own rebellious way, they are curious about values of coinage. traditionally, coins come in values like 1, 5, 10, 20 or 25, 50, and 100 units, sometimes with a 2 unit coin thrown in for good measure.
The cows want to know how many different ways it is possible to dispense a certain amount of money using various coin systems. for instance, using a system of {1, 2, 5, 10 ,...} it is possible to create 18 units several different ways, including: 18x1, 9x2, 8x2 + 2x1, 3x5 + 2 + 1, and other others.
Write a program to compute how many ways to construct a given amount of money using supplied coinage. it is guaranteed that the total will fit into both a signed long (C/C ++) and int64 (free Pascal ).
Program name: moneyinput format
The number of coins in the system is V (1 <= V <= 25 ).
The amount money to construct is n (1 <=n <= 10,000 ).
Line 1:
Two integers, V and N
Lines 2 ..:
V integers that represent the available coins (no participant number of integers per line)
Sample input (File money. In)
3 101 2 5
Output Format
A single line containing the total number of ways to construct n money units using V coins.
Sample output (File money. out)
10 The question is very simple, that is, to give the coin system and find the total number of cases in which V can be spelled out.
You can use DP to solve the problem. Note that money [J] [I] + = money [J-K * sys [I-1] [I-1];
Among them, money [J] [I] indicates the total number of cases where J is spelled out using the first I coins. The obvious requirement for the question is money [N] [v].
Reference code:
/* ID:shiryuw1 PROG:money LANG:C++ */ #include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath> using namespace std; int sys [ 30 ] ; long long money [ 10005 ] [ 30 ] = { 0 } ; int main() { freopen("money.in","r",stdin); freopen("money.out","w",stdout); int v , n ; cin >> v >> n ; int i , j , k ; for ( i = 0 ; i < v ; i ++ ) { cin >> sys [ i ] ; } sort ( sys , sys + v ); for ( i = 0 ; i <= n ; i ++ ) { if ( i % sys [ 0 ] == 0 ) money [ i ] [ 1 ] = 1; else money [ i ] [ 1 ] = 0 ; } for ( i = 2 ; i <= v ; i ++ ) { for ( j = 0 ; j <= n ; j ++ ) { for ( k = 0 ; j >= k * sys [ i - 1 ] ; k ++ ) { money [ j ] [ i ] += money [ j - k * sys [ i - 1 ] ] [ i - 1 ] ; } } } printf ( "%lld\n" , money [ n ] [ v ] ); return 0; }