使用回溯来输出符合条件的所有 n 个数字排列

发布于 2025-01-09 04:37:37 字数 528 浏览 0 评论 0原文

我想做一个算法,读取整数 n、a 和 b,并输出 n 个数字的所有排列,其中数字 a 和 b 是连续的。

例如,如果 n=3,a=1,b=2,它应该输出 123 312。

我使用回溯来找到所有 n 个数字排列,但我不知道应该在哪个函数中以及在哪里放置我的条件,如果那是一件事的话。

void out()
{

   for (int i = 1; i <= n; ++i)
      cout << x[i];


   cout <<" ";
}

void back(int k)
{
   int i;
   for (i = 1; i <= n; ++i)
   {
      if (!p[i])
      {
         x[k] = i;
         p[i] = 1;
         if (k < n)
            back(k+1);
         else
            out();
         p[i] = 0;
      }
   }
}

I want to make an algorithm that reads the integers n, a and b and outputs all the permutations of n numbers where the numbers a and b are consecutive.

For example, if n=3, a=1, b=2 it should output 123 312.

I've used backtracking in order to find all n numbers permutations, but I don't know in what function and where I should put my condition, if that is even a thing.

void out()
{

   for (int i = 1; i <= n; ++i)
      cout << x[i];


   cout <<" ";
}

void back(int k)
{
   int i;
   for (i = 1; i <= n; ++i)
   {
      if (!p[i])
      {
         x[k] = i;
         p[i] = 1;
         if (k < n)
            back(k+1);
         else
            out();
         p[i] = 0;
      }
   }
}

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

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

发布评论

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

评论(1

请远离我 2025-01-16 04:37:37

使用标准算法让您的生活更轻松。特别是,有 std::next_permutation 来获取所有排列。然后,您可以遵循 Jarod42 评论中的建议,并将 12 视为单个元素。这样,任何排列都有 12 相邻:

#include <algorithm>
#include <iostream>
#include <string>

int main() {
    int n = 5;
    int a = 1;
    int b = 3;

    std::vector<std::string> vec;
    for (int i=1; i <= n; ++i) if (i!=a && i!=b) vec.push_back(std::to_string(i));
    vec.push_back(std::to_string(a) + std::to_string(b));
    std::sort(vec.begin(),vec.end());
    
    std::vector<std::vector<std::string>> permutations;
    permutations.push_back(vec);
    while (std::next_permutation(vec.begin(),vec.end())) permutations.push_back(vec);

    for (const auto& permu : permutations) {
        for (const auto& e : permu) std::cout << e;
        std::cout << "\n";
    }
}

或者,不要将 12 视为单个元素并检查循环中的条件:

#include <algorithm>
#include <iostream>
#include <string>

int main() {
    int n = 5;
    int a = 1;
    int b = 3;

    std::vector<int> vec;
    for (int i=1; i <= n; ++i) vec.push_back(i);
    
    std::vector<std::vector<int>> permutations;
    permutations.push_back(vec);
    while (std::next_permutation(vec.begin(),vec.end())) {
        if ( ... a is not adjacent to b ...) continue;
         permutations.push_back(vec);
    }

    for (const auto& permu : permutations) {
        for (const auto& e : permu) std::cout << e;
        std::cout << "\n";
    }
}

If您只需要打印排列,不需要将它们存储在向量中,而是直接打印它们。

Use standard algorithms to make your life easier. In particular, there is std::next_permutation to get all permutations. Then you can either follow the advice in a comment from Jarod42 and treat 12 as a single element. In that way any permutation has 1 and 2 adjacent:

#include <algorithm>
#include <iostream>
#include <string>

int main() {
    int n = 5;
    int a = 1;
    int b = 3;

    std::vector<std::string> vec;
    for (int i=1; i <= n; ++i) if (i!=a && i!=b) vec.push_back(std::to_string(i));
    vec.push_back(std::to_string(a) + std::to_string(b));
    std::sort(vec.begin(),vec.end());
    
    std::vector<std::vector<std::string>> permutations;
    permutations.push_back(vec);
    while (std::next_permutation(vec.begin(),vec.end())) permutations.push_back(vec);

    for (const auto& permu : permutations) {
        for (const auto& e : permu) std::cout << e;
        std::cout << "\n";
    }
}

Alternatively, do not treat 12 as singe element and check the condition in the loop:

#include <algorithm>
#include <iostream>
#include <string>

int main() {
    int n = 5;
    int a = 1;
    int b = 3;

    std::vector<int> vec;
    for (int i=1; i <= n; ++i) vec.push_back(i);
    
    std::vector<std::vector<int>> permutations;
    permutations.push_back(vec);
    while (std::next_permutation(vec.begin(),vec.end())) {
        if ( ... a is not adjacent to b ...) continue;
         permutations.push_back(vec);
    }

    for (const auto& permu : permutations) {
        for (const auto& e : permu) std::cout << e;
        std::cout << "\n";
    }
}

If you only need to print the permutations you need not store them in a vector, but print them directly.

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