不同长度的排列

发布于 2024-10-24 23:29:26 字数 622 浏览 2 评论 0原文

可能的重复:
从 n 返回 k 元素的所有组合的算法

我想要做的是生成所有不同长度的 1-9 排列。例如,长度为 1 的所有排列都可以简单地表示为 {1}、{2}、{3}、{4} .. 等等。 长度为 2 的排列为:{1,2}, {1,3}, {1,4} .. 到目前为止,我一直在使用 std::next_permutation() ,但在这种情况下它无法完成这项工作。

有没有什么办法可以不使用递归来解决这个问题呢?如果没有,并且您提供了任何代码,如果您能向我解释,我将非常感激,因为我现在真的很努力解决递归问题,尤其是自己实现递归解决方案。提前致谢。

Possible Duplicate:
Algorithm to return all combinations of k elements from n

What I want to do is generate all 1-9 permutations of different length. For example all permutations of length one would simply be
{1}, {2}, {3}, {4} .. and so on.
Permutations of length two would be: {1,2}, {1,3}, {1,4} ..
So far I've been using std::next_permutation(), however it won't do the job in this case.

Is there any way to solve this problem without using recursion? If not and you're providing any code I would really appreciate if you would explain to me, because I'm really struggling with recursion right now, especially with implementing recursive solutions myself. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

給妳壹絲溫柔 2024-10-31 23:29:26

我将使用的方法从 hakmem 175 开始,即找到具有相同位数的下一个更高的数字。
假设您在一个 int 中编写了 10 个数字(从位 0 到位 9)
这意味着您必须从 1 循环到 10,并用第一个接近您的数字对的数字来填充您的 start int。

例如:对于 {1},{2} ...起始 int 将为 2^0
对于 {1,2} {1,3} ...起始整数将为 2^0+2^1
等等。

之后,您必须创建一个可以解释该数字的方法(检查是否设置了某个位,然后相应的数字将出现在序列中)。

之后调用 hakmems 方法获取下一个数字:

unsigned nexthi(unsigned a) {
  unsigned c = (a & -a);
  unsigned r = a+c;
  return (((r ^ a) >> 2) / c) | r);
}

并继续循环,直到没有数字剩余。

并转到下一个位数 (1->10)

我建议您查看 hakmem 方法以了解它是如何工作的,如果您了解一些有关位的知识,那么实现很容易。

The approach I would use starts from hakmem 175 and that is finding the next higher number with the same number of bits.
So let's say that you code in one int you 10 numbers (from bit 0 to bit 9)
That means that you have to loop from 1 to 10 and prime your start int with the first number that will approximate your pair.

ex: for {1},{2} ... the start int would be 2^0
for {1,2} {1,3} ... the start int would be 2^0+2^1
and so on.

after that you have to make a method that would interpret the number (check if a bit is set then the corresponding number will appear in the sequence).

after that call hakmems method for the next number:

unsigned nexthi(unsigned a) {
  unsigned c = (a & -a);
  unsigned r = a+c;
  return (((r ^ a) >> 2) / c) | r);
}

and continue the loop until no numbers remain.

and go to the next number of bits (1->10)

I suggest looking to hakmem method for you to understand how it works, the implementation is easy if you know a few things about bits.

花开浅夏 2024-10-31 23:29:26
void permute(std::vector<int>& digits, int length) {
    if (length == 0) {
        std::cout << "{";
        for (int i = 0; i < digits.length; i++) {
            std::cout << digits[i] << ",";
        }
        std::cout << "}" << std::endl;
        return;
    }

    for (int i = 0; i < 9; i++) {
        std::vector<int> newDigits(digits);
        newDigits.push_back(i);
        permute(newDigits, length -1);
    }
}

在 main 中将其称为

std::vector<int> digits;
permute(digits, 10); // For 10 digit permutations. 

现在,如果您想获得长度为 1,2 ... n 的排列,那么您所要做的就是将排列函数放入循环中。

请注意,这不是最有效的实现。但我想这应该清楚地说明了递归。

void permute(std::vector<int>& digits, int length) {
    if (length == 0) {
        std::cout << "{";
        for (int i = 0; i < digits.length; i++) {
            std::cout << digits[i] << ",";
        }
        std::cout << "}" << std::endl;
        return;
    }

    for (int i = 0; i < 9; i++) {
        std::vector<int> newDigits(digits);
        newDigits.push_back(i);
        permute(newDigits, length -1);
    }
}

Call this within main as

std::vector<int> digits;
permute(digits, 10); // For 10 digit permutations. 

Now if you want to get permutations of length 1,2 ... n, then all you have to do is put the permute function inside a loop.

Note that this is not the most efficient of implementations. But I suppose this should illustrate the recursion clearly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文