如何购买物品的组合
我试图找到一种方法来计算(在 C++ 中),给定一个价格不变的物品列表,如果一个人有 X 美元,他可以通过多少种方式购买 X 数量的物品。
到目前为止,我已经尝试使用嵌套 for 循环来尝试暴力破解解决方案,但是,我觉得我可能会错过一个我似乎看不到的非常简单的解决方案。
任何帮助将非常感激。 谢谢。
I'm trying to find a way to figure out (in c++), given a list of items with a constant price, how many ways a person can buy X amount of items if they have X amount of dollars.
So far, I have attempted to use nested for loops to try and brute force a solution, however, I feel i might be missing a very simple solution that I can't seem to see.
Any help would be very much appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这与常见的编程问题非常相似:“有多少种方法可以将 Y 类型的硬币与 Z 值组合起来赚取 X 美元”,即用 Y 类型的硬币找 X 美元。
这是一个可以移植到 C++ 的通用解决方案:
用 X 美元,您可以从调用开始:
This is very similar to the common programming question: "How many ways can you combine Y types of coins with Z values to make X dollars", i.e. make change for X dollars with Y coin types.
Here's a general solution that could be ported to C++:
With X dollars, you start off with the call: