Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.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停止#_C#_Arrays_Combinations - Fatal编程技术网

C# 数组的所有组合,在三级C停止#

C# 数组的所有组合,在三级C停止#,c#,arrays,combinations,C#,Arrays,Combinations,我正在尝试循环一个数组,并获得所有可能的组合,但我需要它在三个级别后停止。例如: String[] arr = ["Service1", "Service2", "Service3", "Service4"]; 阵列中可能有四个以上,但从本例中,我希望能够生成以下组合: Service1, Service2, Serivce3 Service1, Service2, Serivce4 Service1, Service3, Serivce2 Service1, Service3, Serivc

我正在尝试循环一个数组,并获得所有可能的组合,但我需要它在三个级别后停止。例如:

String[] arr = ["Service1", "Service2", "Service3", "Service4"];
阵列中可能有四个以上,但从本例中,我希望能够生成以下组合:

Service1, Service2, Serivce3
Service1, Service2, Serivce4
Service1, Service3, Serivce2
Service1, Service3, Serivce4
Service1, Service4, Serivce2
Service1, Service4, Serivce3

Service2, Service1, Serivce3
Service2, Service1, Serivce4
Service2, Service3, Serivce1
Service2, Service3, Serivce4
Service2, Service4, Serivce3
Service2, Service4, Serivce1

Service3, Service1, Serivce2
Service3, Service1, Serivce4
Service3, Service2, Serivce1
Service3, Service2, Serivce4
Service3, Service4, Serivce2
Service3, Service4, Serivce1

Service4, Service2, Serivce3
Service4, Service2, Serivce1
Service4, Service3, Serivce2
Service4, Service3, Serivce1
Service4, Service1, Serivce2
Service4, Service1, Serivce3

到目前为止,我所做的尝试和研究并没有给我这些结果,我非常感谢你们能提供的任何帮助

巧合的是,我昨天写了一个程序作为编码挑战,注意到我删除了重复项,例如“pop”有3个!=6个字母排列,但“p”重复了两次且无法区分,因此为3/2=3(即“opp”、“pop”、“ppo”)

它打印出以下内容:

abc、abd、acb、acd、adb、adc、bac、bad、bca、bcd、bda、bdc、cab、cad、cba、cbd、cda、cdb、dab、dac、dba、dbc、dca、dcb

计数24,即4*3*2=24,与预期完全一致。现在,将每个字符映射到字符串应该很容易,不过我建议改为修改方法本身。

您可以采用并使用组合类,如本例所示:

char[] inputSet = { 'A', 'B', 'C', 'D' };

var combinations = new Combinations<char>(inputSet, 3);
var cformat = "Combinations of {{A B C D}} choose 3: size = {0}";
Console.WriteLine(String.Format(cformat, combinations.Count));

foreach(var combination in combinations)
{
    Console.WriteLine(String.Join(", ", combination);
}
char[]inputSet={'A','B','C','D'};
var组合=新组合(输入集,3);
var cformat=“组合{{A B C D}选择3:size={0}”;
WriteLine(String.Format(cformat,combines.Count));
foreach(组合中的var组合)
{
Console.WriteLine(String.Join(“,”,组合);
}

这里是另一个解决方案:

var strings = new[] { "a", "b", "c", "d" };
var combinations = (
from s1 in strings
from s2 in strings.Where(s => s != s1)
from s3 in strings.Where(s => s != s2 && s != s1)
select new { s1, s2, s3 }).Distinct();

foreach (var c in combinations)
{
    Console.WriteLine($"{c.s1}{c.s2}{c.s3}");
}
也许,代码可以再润色一点,但它是有效的


希望这会有所帮助。

因为,显然顺序很重要(
Service3,Service4,Service2
Service4,Service2,Serivce3
是独立的结果),那么您在这里指的通常是排列,而不是组合(组合类似于排列,但顺序并不重要)。你可能会更幸运地使用它进行谷歌搜索。这个答案是错误的,至少OP是这样问的。它将打印出
{abc}、{abd}、{abd}、{abcd}、{bcd}
,即4选择3=4个结果。显然,顺序在这里很重要,因为OP发布了一个包含24个结果的列表。你应该使用
变体
类(在您复制/粘贴的代码正下方的部分)。您的
控制台有问题。WriteLine($“{c.s1}{c.s2}{c.s3}”);
行,因为这不会编译。这取决于您使用的是哪个版本的c。啊,我的错误。我没有跟上最新的c#版本…我仍然有Visual Studio 2010在工作。O
char[] inputSet = { 'A', 'B', 'C', 'D' };

var combinations = new Combinations<char>(inputSet, 3);
var cformat = "Combinations of {{A B C D}} choose 3: size = {0}";
Console.WriteLine(String.Format(cformat, combinations.Count));

foreach(var combination in combinations)
{
    Console.WriteLine(String.Join(", ", combination);
}
var strings = new[] { "a", "b", "c", "d" };
var combinations = (
from s1 in strings
from s2 in strings.Where(s => s != s1)
from s3 in strings.Where(s => s != s2 && s != s1)
select new { s1, s2, s3 }).Distinct();

foreach (var c in combinations)
{
    Console.WriteLine($"{c.s1}{c.s2}{c.s3}");
}