1371. Currency system (complete knapsack problem)

AcWing: 1371. Currency System

Insert picture description here


Question type: 3. Complete knapsack problem

dp problem

There is no limit to the number of times the currency is used each time. Complete knapsack problem (the maximum value of items that can be obtained under a certain capacity when solving)

And here is the number of plans



AC Code

import java.util.*;
import static java.lang.System.out;

public class Main{
    
    
    
    public static void main(String[] args) {
    
    
        Scanner in = new Scanner(System.in);
        int v = in.nextInt();
        int n = in.nextInt();
        int[] w = new int[v];
        for(int i = 0; i < v; i++) w[i] = in.nextInt();
        
        // 方案数很大 
        long [] dp = new long[n + 1];
        dp[0] = 1;
        
        //个数
        for(int i = 0; i < v; i++) {
    
    
            // 面值
            for(int j = w[i]; j <= n; j++) {
    
    
                dp[j] += dp[j - w[i]];
                // 完全背包问题: dp[j] = Math.max(dp[j], dp[j - w[i]] + w[i]);
            }
        }
        
        out.println(dp[n]);
    }
    
}



Guess you like

Origin blog.csdn.net/qq_43765535/article/details/113038947