b********d 发帖数: 393 | 1 假设银行有无限量的1分钱,5分钱,10分钱和25分钱的硬币。如果客户给出任意一个取
款数额,求银行总共有几种兑现方法。
例如客户取37分钱。可能的付款方式是:
25+10+1+1
25+5+5+1+1
25+5+1x7
25+1x12
10+10+5+11
.。。。。
试着用递归(recursive function)但是边界条件定义不出来。请高手指点一二,给一
个Pseudo-Code,不胜感激。 | r*******n 发帖数: 3020 | 2 This question is the example counting change from the book of SCIP.
the following is my python code.
# Counting change
# kinds_of_change = [1, 5, 10, 25]
def count_change(amount, kinds_of_changes):
if amount == 0:
return 1
if amount < 0 or not kinds_of_changes:
return 0
else:
return count_change(amount, kinds_of_changes[1:]) +
count_change(amount - kinds_of_change[0], kinds_of_change)
【在 b********d 的大作中提到】 : 假设银行有无限量的1分钱,5分钱,10分钱和25分钱的硬币。如果客户给出任意一个取 : 款数额,求银行总共有几种兑现方法。 : 例如客户取37分钱。可能的付款方式是: : 25+10+1+1 : 25+5+5+1+1 : 25+5+1x7 : 25+1x12 : 10+10+5+11 : .。。。。 : 试着用递归(recursive function)但是边界条件定义不出来。请高手指点一二,给一
| l********a 发帖数: 1154 | 3 只有1,5,10,25这4中,用循环也可以解决
欧拉工程有一题类似,7个coins,循环都可以解决
楼上的递归是标准解法 | b********d 发帖数: 393 | 4 谢谢楼上两位~~
自己做出来了,用递归的方式,数学表达式为:
fun(amt, [25, 10, 5, 1])=
fun(amt - 25, [25, 10, 5, 1])
+ fun(amt - 10, [10, 5, 1])
+ fun(amt - 5, [5, 1])
+ fun(amt - 1, [1]) | l******u 发帖数: 1174 | 5 似可简化为:
fun(amt, [25, 10, 5, 1])=
fun(amt - 25, [25, 10, 5, 1])
+ fun(amt, [10, 5, 1])
【在 b********d 的大作中提到】 : 谢谢楼上两位~~ : 自己做出来了,用递归的方式,数学表达式为: : fun(amt, [25, 10, 5, 1])= : fun(amt - 25, [25, 10, 5, 1]) : + fun(amt - 10, [10, 5, 1]) : + fun(amt - 5, [5, 1]) : + fun(amt - 1, [1])
| i***h 发帖数: 12655 | 6 what is book of SCIP? Thanks
【在 r*******n 的大作中提到】 : This question is the example counting change from the book of SCIP. : the following is my python code. : # Counting change : # kinds_of_change = [1, 5, 10, 25] : def count_change(amount, kinds_of_changes): : if amount == 0: : return 1 : if amount < 0 or not kinds_of_changes: : return 0 : else:
|
|