gpt4 book ai didi

algorithm - 如何在给定一些美元值(value)时找到所有硬币组合

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:11:33 25 4
gpt4 key购买 nike

<分区>

我找到了几个月前为面试准备编写的一段代码。

根据我的评论,它试图解决这个问题:

Given some dollar value in cents (e.g. 200 = 2 dollars, 1000 = 10 dollars), find all the combinations of coins that make up the dollar value. There are only pennies (1¢), nickels (5¢), dimes (10¢), and quarters (25¢) allowed.

例如,如果给出 100,则答案应为:

4 quarter(s) 0 dime(s) 0 nickel(s) 0 pennies  
3 quarter(s) 1 dime(s) 0 nickel(s) 15 pennies
etc.

我相信这可以通过迭代和递归两种方式解决。我的递归解决方案有很多问题,我想知道其他人会如何解决这个问题。这个问题的困难部分是使其尽可能高效。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com