Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 三位数的递归置换_C#_Algorithm_Recursion_Backtracking_Recursive Backtracking - Fatal编程技术网

C# 三位数的递归置换

C# 三位数的递归置换,c#,algorithm,recursion,backtracking,recursive-backtracking,C#,Algorithm,Recursion,Backtracking,Recursive Backtracking,我正在递归地寻找一个3位数的所有排列 我已经厌倦了以下排列方法: static int a = 1; static int b = 2; static int c = 3; static int aCount; static int bCount; static int cCount; static void perm(int a, int b, int c) { Console.WriteLine("( {0}

我正在递归地寻找一个3位数的所有排列

我已经厌倦了以下排列方法:

    static int a = 1;
    static int b = 2;
    static int c = 3;
    static int aCount;
    static int bCount;
    static int cCount;

    static void perm(int a, int b, int c)
    {

        Console.WriteLine("( {0}, {1}, {2} )", a, b, c); // (1,2,3 )

        if (aCount < 1 && bCount<1 &&cCount<1)
        {
            aCount++;

            perm(a, c, b);
        }
        else
            if (aCount==1 && bCount < 1 && cCount<1)
            {

                bCount++;
                perm(b, a, c);
            }
            else
                if (aCount == 1 && bCount == 1 && cCount < 1)
                 {
                    perm(b,c,a);
                  }
        else
                if (aCount==1 && bCount==1 && cCount < 1)
                {
                    cCount++;
                    perm(c, a, b);  //c b a

                }
               else
                    if (aCount == 1 && bCount == 1 &&  cCount == 1)
                {
                    perm(c, b, a);

                }

            }
static int a=1;
静态int b=2;
静态int c=3;
静态整数;
静态整数b计数;
静态整数帐户;
静态void perm(inta、intb、intc)
{
Console.WriteLine(({0},{1},{2})”,a,b,c);/(1,2,3)

如果(aCount<1&&bCount您说您正在尝试递归,但所有这些行都出现在您的代码中:

perm(a, c, b)
perm(b, a, c)
perm(b, c, a)
perm(c, a, b)
perm(c, b, a)
当然还有函数的第一个调用:
perm(a,b,c)

这样做容易得多:

static void perm(int a, int b, int c)
{
    Console.WriteLine("( {0}, {1}, {2} )", a, b, c);
    Console.WriteLine("( {0}, {2}, {1} )", a, b, c);
    Console.WriteLine("( {1}, {0}, {2} )", a, b, c);
    Console.WriteLine("( {1}, {2}, {0} )", a, b, c);
    Console.WriteLine("( {2}, {0}, {1} )", a, b, c);
    Console.WriteLine("( {2}, {1}, {0} )", a, b, c);
}

首先,这两种情况中的任何一种都会导致无限递归:

if (aCount == 1 && bCount == 1 && cCount < 1)
{
    perm(b,c,a);
}
原因是您没有更改
a账户
b账户
c账户
,因此您最终会重复触发相同的案例

除此之外,您似乎并没有递归地思考这个问题——正如在另一个答案中提到的,所有的排列都出现在一个调用中,因此,如果您这样做,您的递归深度将为2,这可能本质上涉及到用print语句替换每个递归调用,以及使用非递归函数开

对于递归解决方案,请尝试考虑一种解决方案,其中处理当前调用中的单个字符,然后递归到下一个

如果您无法理解,请提供更详细的解释:

  • 从第一个字符开始
  • 尝试将当前字符与每个剩余字符交换(包括其本身,即不执行任何操作)
  • 递归到下一个字符
  • 在最后一个字符处,打印出所有字符。

  • 提示-将数组和当前索引传递给函数


    我将为您编写伪代码:

    permutationABC()
    {
        int n=3;
        array SOL[n]/* 1 if you get the element  otherwise 0 if you don't get the element , the meaning of SOL is that SOL[0] is 1 if we had got 'a' , otherwise 0. It's the same for the others */
        initSolWithAllZero(SOL)
        permRecursive(abc,SOL,0,n);
    }
    
    permRecursive(SOL,i,n)
    {
        if i == n then print(SOL) 
        else
        {
            for k=0 to n
            {
                 if SOL[k] == 0 then
                 {
                      SOL[k]=1 // take 
                      permRecursive(SOL,i+1,n)
                      SOL[K]=0 // don't take ... go back....
                 }
            }
        }
    }
    

    时间是O(n*n!)O(n!)是排列的数量,O(n!)是是打印时间。

    请在开始之前将aCount、bCount和cCount初始化为0。哦,对不起,我一定错过了,当我在试验时:PI希望是这样!我被要求编写一个递归排列函数,只是:我需要一种递归方式,为每个排列打印行。
    permutationABC()
    {
        int n=3;
        array SOL[n]/* 1 if you get the element  otherwise 0 if you don't get the element , the meaning of SOL is that SOL[0] is 1 if we had got 'a' , otherwise 0. It's the same for the others */
        initSolWithAllZero(SOL)
        permRecursive(abc,SOL,0,n);
    }
    
    permRecursive(SOL,i,n)
    {
        if i == n then print(SOL) 
        else
        {
            for k=0 to n
            {
                 if SOL[k] == 0 then
                 {
                      SOL[k]=1 // take 
                      permRecursive(SOL,i+1,n)
                      SOL[K]=0 // don't take ... go back....
                 }
            }
        }
    }