uva 674 Coin Change [Complete Backpack]

Question link: https://vjudge.net/contest/59424#problem/A

Topic meaning:

There are 5 kinds of coins, the denominations are 1, 5, 10, 25, 50. Now give the amount and ask how many ways the denomination can be composed.

 

Problem solving ideas:

First of all, we can think of how many kinds of 11 are made of these coins.

It is the number of species that make up 10, plus the number of species that make up 6, plus the number of kinds that make up 1, because these denominations are all plus a coin to get 11.

Then we can continue to find the number of 1 to form 10, then it is obviously the sum of the composition of 9, 5, and 0.

It should be noted that 1+5 is a bottom-up method. It should be noted that 1+5 and 5+1 are the same, so it is necessary to deal with it, and it will not be wrong to arrange from small to large.

 

I don't understand this question very well, I'll look at it later.                     Reprinted in >>>     

 

Memorized search : Very idiotic algorithm, directly handed over to the next layer to calculate, and recorded to avoid repeated calculation later.

#include <cstdio>  
#include <cstring>  
const int MAXN = 8000;  
const int coin[5] = {1, 5, 10, 25, 50};  
int n;  
long long dp[MAXN][5];  
  
long long solve(int i, int s) {  
    if (dp[s][i] != -1)  
        return dp[s][i];  
    dp[s][i] = 0;  
    for (int j = i; j < 5 && s >= coin[j]; j++)  
        dp[s][i] += solve(j, s - coin[j]);  
    return dp[s][i];  
}  
  
int main() {  
    memset(dp, -1, sizeof(dp));  
    for (int i = 0; i < 5; i++)  
        dp[0][i] = 1;  
    while (scanf("%d", &n) != EOF)  
        printf("%lld\n", solve(0, n));  
    return 0;  
}  

 

Recursion : bottom-up method, it should be noted that 1+5 and 5+1 are one kind, so it is necessary to deal with it, and it will not be wrong to arrange from small to large.

#include <cstdio>  
const int MAXN = 8000;  
int n, coin[5] = {1, 5, 10, 25, 50};  
long long dp[MAXN] = {1};  
  
int main() {  
    for (int i = 0; i < 5; i++)  
        for (int j = 0; j < MAXN - 100; j++)  
            dp[j + coin[i]] += dp[j];  
  
    while (scanf("%d", &n) != EOF)  
        printf("%lld\n", dp[n]);  
    return 0;  
}  

 

2018-04-30

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325090683&siteId=291194637