The Coin Change Problem

本文介绍了一种使用动态规划算法解决找零问题的方法,即计算给定面额的硬币可以组成特定金额的不同方式的数量。通过三个嵌套循环实现动态规划解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Statement

How many different ways can you make change for an amount, given a list of coins? In this problem, your code will need to efficiently compute the answer.

Problem Statement

Write a program that, given two arguments to STDIN

  • a list of coins c1, c2, c3, ..
  • and an amount n

Prints out how many different ways you can make change from the coins to STDOUT.

The problem can be formally stated:

Given a value N, if we want to make change for N cents, and we have infinite supply of each of C = { C1, C2, .. , Cm} valued coins, how many ways can we make the change? The order of coins doesn’t matter.

Example 1:

For N = 4 and C = {1,2,3} there are four solutions: {1,1,1,1},{1,1,2},{2,2},{1,3}

So given the input

1, 2, 3 4

your program should output:

4

用动态规划实现,三个循环搞定  dp[j][k]表示大小为 k 的数分成 j 部分之和

for(int i=0;i<A.size();i++)
   for(int j=1;j<=m;j++)
	for(int k=A[i];k<=N;k++) dp[j][k]+=dp[j-1][k-A[i]];




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值