C++--Complete knapsack problem

1. [Template] Complete Backpack_Niuke Question Master_Niuke.com

You have a knapsack with a maximum volume of V.

Now there are n kinds of items, and there are any number of each item. The volume of the i-th item is vivi​, and the value is wiwi​.

(1) Find the maximum value of items that this backpack can hold?

(2) If the knapsack is just full, how much value can it hold at most?

Enter a description:

The two integers n and V in the first line represent the number of items and the volume of the backpack.

The next n lines, each with two numbers vivi​and wiwi​, represent the volume and value of the i-th item.

1≤n,V≤10001≤n,V≤1000

Output description:

The output has two lines. The first line outputs the answer to the first question, and the second line outputs the answer to the second question. If there is no solution, please output 0.

Example 1

enter:

2 6
5 10
3 1
output:
10
2

Example 2

enter:

3 8
3 10
9 1
10 1
output:
20
0

illustrate:

Can't exactly fill the backpack.

Example 3

enter:

6 13
13 189
17 360
19 870
14 184
6 298
16 242
output:
596
189

illustrate:

It can hold 2 items of No. 5, reaching the maximum value of 298*2=596. If it is required to be filled exactly, only one item of No. 1 can be loaded, and the value is 189.
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    int n, V;
    cin >> n >> V;
    int v[n];
    int w[n];
    for (int i = 0; i < n; i++) cin >> v[i] >> w[i];
    int dp[n + 1][V + 1];
    //初始化
    memset(dp, 0, sizeof dp);
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j <= V; j++)
        {
            //动态转移方程
            dp[i][j] = dp[i - 1][j];
            if (j - v[i - 1] >= 0) dp[i][j] = max(dp[i][j], dp[i][j - v[i - 1]] + w[i - 1]);
        }
    }
    cout << dp[n][V] << endl;

    memset(dp, 0, sizeof dp);
    for (int i = 1; i <= V; i++) dp[0][i] = -1;
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0; j <= V; j++)
        {
            dp[i][j] = dp[i - 1][j];
            if (j - v[i - 1] >= 0 && dp[i][j - v[i - 1]] != -1) dp[i][j] = max(dp[i][j], dp[i][j - v[i - 1]] + w[i - 1]);
        }
    }
    cout << (dp[n][V] == -1 ? 0 : dp[n][V]) << endl;
    return 0;
}

2. LeetCode  official website - a technology growth platform loved by global geeks

You are given an array of integers coinsrepresenting coins of different denominations and an integer amountrepresenting the total amount.

Calculates and returns the minimum number of coins required to make up the total amount . If no combination of coins can make up the total amount, return  -1.

You can consider the amount of each coin to be infinite.

Example 1:

Input: coins = [1, 2, 5], amount = 11
Output: 3 
Explanation: 11 = 5 + 5 + 1

Example 2:

Input: coins = [2], amount = 3
Output: -1

Example 3:

Input: coins = [1], amount = 0
 Output: 0
class Solution {
public:
    int coinChange(vector<int>& coins, int amount) 
    {
        int n=coins.size();
        vector<int> dp(amount+1);//创建dp表
        for(int i=1;i<=amount;i++) dp[i]=0x3f3f3f3f;//初始化,寻找最小值
        for(int i=1;i<=n;i++)
        {
            for(int j=coins[i-1];j<=amount;j++)
            {
                
                if(j-coins[i-1]>=0)
                {
                    dp[j]=min(dp[j],dp[j-coins[i-1]]+1);//动态转移方程
                }
            }
        }
        return dp[amount]>=0x3f3f3f3f?-1:dp[amount];//返回值
    }
};

3. Exchange change for  LeetCode (LeetCode) official website - a technology growth platform loved by global geeks

You are given an array of integers coinsrepresenting coins of different denominations and an array of integers amountrepresenting the total amount.

Please calculate and return the number of coin combinations that can make up the total amount. If no combination of coins can make up the total amount, return 0.

Suppose there are an infinite number of coins of each denomination. 

The title data guarantees that the result fits into a 32-bit signed integer.

Example 1:

Input: amount = 5, coins = [1, 2, 5]
 Output: 4
 Explanation: There are four ways to make up the total amount: 
5=5 
5=2+2+1 
5=2+1+1+1 
5 =1+1+1+1+1

Example 2:

Input: amount = 3, coins = [2]
 Output: 0
 Explanation: The total amount 3 cannot be made up only with coins of denomination 2.

Example 3:

Input: amount = 10, coins = [10] 
 Output: 1
class Solution {
public:
    int change(int amount, vector<int>& coins) 
    {
        int n=coins.size();
        vector<int> dp(amount+1);
        dp[0]=1;//初始化
        for(int i=1;i<=n;i++)
        {
            for(int j=coins[i-1];j<=amount;j++)
            {
                
                dp[j]+=dp[j-coins[i-1]];//动态转移方程
            }
        }
        return dp[amount];//返回值
    }
};

4.  The official website of LeetCode - a technology growth platform loved by global geeks

Given an integer n, return the least number of perfect squares that sum ton .

A perfect square is an integer whose value is equal to the square of another integer; in other words, whose value is equal to the product of an integer multiplied by itself. For example, 1, 4, 9and 16are all perfect squares, but 3and 11are not.

Example 1:

Input: n = 12
Output: 3 
 Explanation:12 = 4 + 4 + 4

Example 2:

Input: n = 13
Output: 2
 Explanation:13 = 4 + 9
class Solution {
public:
    int numSquares(int n) 
    {
        int m=sqrt(n);
        vector<int> dp(n+1,0x3f3f3f3f);//初始化
       
        dp[0]=0;
        for(int i=1;i<=m;i++)
        {
            for(int j=i*i;j<=n;j++)
            {
                dp[j]=min(dp[j],dp[j-i*i]+1);//动态转移方程
            }
        }
        return dp[n];       
    }
};

Guess you like

Origin blog.csdn.net/weixin_66828150/article/details/132611202