生成按各个数字之和排序的 n 位数字(无递归)
我希望按以下顺序生成 n 位数字的所有可能值,其中顺序由各个数字的总和决定。
例如,对于n = 3
:
111 sum = 3
112 sum = 4
121
211
122 sum = 5
212
221
113
131
311
114 sum = 6
141
411
:::
999 sum = 27
求和组内的顺序并不重要。
任何帮助,想法将不胜感激
I'm looking to generate all possible values of n-digit number, in the following order, where the sequence is dictated by the sum of the individual digits.
For example, with n = 3
:
111 sum = 3
112 sum = 4
121
211
122 sum = 5
212
221
113
131
311
114 sum = 6
141
411
:::
999 sum = 27
The order within the sum group is not important.
Any help, ideas would be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您维护自己的重要数据堆栈,那么您总是可以将递归问题转变为迭代问题 - 也就是说,如果避免递归的原因是该语言不支持它。
但是,如果语言确实支持它,那么递归解决方案要优雅得多。
我能想到的避免递归的唯一另一个原因是堆栈深度有限。在这种情况下,递归解决方案的迭代转换将不需要太多的堆栈空间来缓解该问题。
但您需要了解,处理 n 个数字的堆栈深度仅相对于 log10n 增长。换句话说,每个数字只能获得一个额外的堆栈帧(只有 10 个堆栈帧可以处理全范围的 32 位整数)。
旁白:当你到达这一点时,你的算法将花费很长时间来运行,堆栈帧将是你的问题中最少的:-)
这是一个递归的Python解决方案:
它输出(对于 2 和 3):
请记住,解决方案仍然可以进行一定程度的优化 - 我将其保留为初始形式,以展示递归是多么优雅。接下来是纯迭代解决方案,但我仍然更喜欢递归解决方案。
运行以下程序并在 UNIX 下使用
sort
和awk
获得所需的顺序。例如:请注意,这使用外部工具进行排序,但您也可以在 C 代码中轻松排序(内存允许)。
You can always turn a recursive problem into an iterative one if you maintain your own stack of important data - that's if the reason for avoiding recursion is that the language doesn't support it.
But, if the language does support it, then recursive solutions are far more elegant.
The only other reason I can think of for avoiding recursion is limited stack depth. In that case an iterative conversion of a recursive solution will mitigate the problem by not requiring as much stack space.
But you need to understand that the stack depth for processing n numbers only grows relative to log10n. In other words, you only get an extra stack frame per digit (only 10 stack frames to handle the full range of 32-bit integers).
Aside: by the time you get to that point, you're algorithm will be taking so long to run, stack frames will be the least of your problems :-)
Here's a recursive Python solution:
which outputs (for 2 and 3):
Keep in mind that solution could still be optimized somewhat - I left it in its initial form to show how elegant recursion can be. A pure-iterative solution follows, but I still prefer the recursive one.
Run the following program and use
sort
andawk
under UNIX to get the desired order. For example:Note that this uses external tools to do the sorting but you could just as easily sort within the C code (memory permitting).
您可以使用 std::next_permutation 吗?
请参阅:上一个答案
Can you use std::next_permutation?
See this: previous SO answer
如果只要有一个模式,使用什么模式并不重要(从您的帖子中并不能完全清楚您是否有特定的模式),那么对于 n=3,从
111
开始并递增,直到达到999
。顺便说一句,您所要求的术语并不完全是“排列”。
If it doesn't matter what pattern you use so long as there is a pattern (it's not entirely clear from your post whether you have a specific pattern in mind) then for n=3, start with
111
and increment until you reach999
.By the way, the term for what you're asking for isn't exactly "permutations".
您可以尝试将问题简化为两个桶:
两个桶的拆分很简单:从桶 A 中全部减一、桶 B 中减 1 开始,然后将 A 中的 1 放入 B 中,直到 A 只包含 1。
三个存储桶拆分就是:从存储桶 A 中全部减去 2 开始,B 和 C 中各减 1。将 A 减 1,并收集 B 和 C 中所有两个存储桶拆分为 3 个,重复直到 A 只包含 1。
You can try to reduce your problem to two buckets:
Two bucket splits are simple: Start with all minus one in bucket A and one in bucket B, then put one from A into B until A contains only one.
Three bucket splits are then just: Start with all minus two in bucket A and one each in B and C. Reduce A by one and collect all two bucket splits of three in B and C, repeat until A contains only one.