#include<bits/stdc++.h> #define ll long long using namespace std; ll count( ll S[], ll m, ll n ) { ll i, j, x, y; ll table[n+1][m]; for (i=0; i<m; i++) table[0][i] = 1; for (i = 1; i < n+1; i++) { for (j = 0; j < m; j++) { x = (i-S[j] >= 0)? table[i - S[j]][j]: 0; y = (j >= 1)? table[i][j-1]: 0; table[i][j] = x + y; } } return table[n][m-1]; } int main() { ll a[] = {1,5,10,25,50}; ll m = 5; ll n; while(cin>>n){ ll ans; ans = count(a,m,n); if(ans>1){ cout<<"There are "<<ans<<" ways to produce "<<n<<" cents change."<<'\n'; } else cout<<"There is only "<<ans<<" way to produce "<<n<<" cents change."<<'\n'; } return 0; }
Friday, February 24, 2017
UVa 357
Labels:
UVA Solution
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Thank you for commenting. Please wait for response :)