//ACM模式 let readline = require('readline') const rl = readline.createInterface({ input:process.stdin, output:process.stdout }); let inputArr = [] rl.on('line',function(line){ inputArr.push(line.split(' ')) }).on('close',function(){ let n = parseInt(inputArr[0][0]) let v = parseInt(inputArr[0][1]) let weight = [] let value = [] for (let i = 1;i<=n;i++){ weight.push(parseInt(inputArr[i][0])) value.push(parseInt(inputArr[i][1])) } let dp = Array(v+1).fill(0) for (let i=0;i<=n;i++){ //这边这个循环要注意,总写错j的起始值 for(let j=weight[i];j<=v;j++){ dp[j] = Math.max(dp[j],dp[j-weight[i]]+value[i]) } } console.log(dp[v]) })
var combinationSum4 = function(nums, target) { let n = target let m = nums.length let dp = Array(n+1).fill(0) dp[0] = 1 // 背包 for (let i=0;i<=n;i++){ // 物品 for (let j=0;j<m;j++){ if (i-nums[j] >=0) dp[i] += dp[i - nums[j]] } } return dp[n] };
rl.on('line',function(line){ var tokens = line.split(' ') let n = parseInt(tokens[0]) let m = parseInt(tokens[1]) let dp = Array(n+1).fill(0) dp[0] = 1 for (let i=0;i<=n;i++){ for (let j=1;j<=m && i-j>=0;j++){ dp[i] += dp[i-j] } } console.log(dp[n]) })
var coinChange = function(coins, amount) { let n = amount let m = coins.length let dp = Array(n+1).fill(Number.MAX_SAFE_INTEGER) dp[0] = 0 for(let i=0;i<m;i++){ console.log(dp) for(let j=coins[i];j<=n;j++){ dp[j] = Math.min(dp[j],dp[j-coins[i]]+1)