Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.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#_.net_Permutation - Fatal编程技术网

C# 创建字符串置换

C# 创建字符串置换,c#,.net,permutation,C#,.net,Permutation,为什么这不能产生正确数量的字符串排列?为了 perm("ABC", 3) 它应该打印27种不同的排列 private static List<string> permutated = new List<string>(30000); public static List<string> perm(string s, int k) { return comb(s, "", k); } private static List<string> pe

为什么这不能产生正确数量的字符串排列?为了

perm("ABC", 3)
它应该打印27种不同的排列

private static List<string> permutated = new List<string>(30000);

public static List<string> perm(string s, int k) { return comb(s, "", k); }

private static List<string> perm(string s, string prefix, int k)
{
    if (k == 0)
    {
        permutated.Add(prefix);
    }
    else 
    {
        for (int i = 0; i < s.Length; i++)
        {
            perm( s.Substring( i + 1 ), prefix + s[i], k - 1 );
        }
    }
    return permutated;
}

您已经在程序中计算了。对于combABC,3应该有一个结果

如果希望得到六个结果,请将递归函数调用中的s.Substringi+1替换为s.Substring0,i+s.Substringi+1


如果您想要27个结果,只需传递s而不是s.子字符串i+1。

调试的尝试向您展示了什么?你试过做什么?ABC有6个排列,不是27个ABC,ACB,BAC,BCA,CAB,CBA第二,除了返回它,你从来没有对排列做过任何事情。嘘,我需要的是组合,不是排列。是的,我将它添加到列表中……好的,那么我如何生成所有可能的组合呢?AAA、BBB、CCC、ABA、ACA。我认为前突变是不同的,但组合有重复的字母