由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 向编程大牛请教一个算法题,谢谢
相关主题
[合集] 问个递归的问题树的前序遍历
[合集] 二叉树的实现求教一个perl问题
这题怎么搞?做题,级数求和
新手问,大家平时使用recursion么?感觉很酷啊装个机快被搞死了
a template counting - anybody understand this?用pseudo-code 写数据结构问题。
reverse LL recursively请教一个递归程序的问题
Python: What does this mean?请问遍历树可以用for loop来完成吗?
recurvion真的很难懂~~怎样记数多次递归调用种某项操作的次数?
相关话题的讨论汇总
话题: change话题: 25话题: kinds话题: amt话题: 10
进入Programming版参与讨论
1 (共1页)
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:

1 (共1页)
进入Programming版参与讨论
相关主题
怎样记数多次递归调用种某项操作的次数?a template counting - anybody understand this?
请教registerreverse LL recursively
谁能推荐一个read-writer lock的C++实现? (转载)Python: What does this mean?
算24的程序recurvion真的很难懂~~
[合集] 问个递归的问题树的前序遍历
[合集] 二叉树的实现求教一个perl问题
这题怎么搞?做题,级数求和
新手问,大家平时使用recursion么?感觉很酷啊装个机快被搞死了
相关话题的讨论汇总
话题: change话题: 25话题: kinds话题: amt话题: 10