Advertisement
ryuk2603

UVa/674(CoinChange)

Nov 1st, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.73 KB | None | 0 0
  1. // UVa/674: Coin Change
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5. #define LL long long
  6.  
  7. LL values[] = { 50, 25, 10, 5, 1 };
  8. LL n = 5;
  9.  
  10. LL coinChange(LL amount)
  11. {
  12.     LL dp[n+1][amount+1];
  13.  
  14.     for(LL i=0; i<=n; i++) dp[i][0] = 1;
  15.  
  16.     for(LL j=1; j<=amount; j++) dp[0][j] = 0;
  17.  
  18.     for(LL i=1; i<=n; i++){
  19.         for(LL j=1; j<=amount; j++){
  20.             if(values[i-1] <= j)
  21.                 dp[i][j] = dp[i-1][j] + dp[i][j - values[i-1]];
  22.             else
  23.                 dp[i][j] = dp[i-1][j];
  24.         }
  25.     }
  26.     return dp[n][amount];
  27. }
  28.  
  29. int main()
  30. {
  31.     LL amount;
  32.     while(scanf("%lld", &amount) == 1) {
  33.         LL solution = coinChange(amount);
  34.         printf("%lld\n", solution);
  35.     }
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement