2017-03-16 38 views
1

我想通过一个数组循环,并获得每个可能的组合,但我需要它在三个级别后停止。例如:数组的所有组合,停在三个级别C#

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 

什么我已经尝试和研究过这些结果,但我不会非常感谢您提供的任何帮助。

+1

'*不给我这些结果*' - 你的代码给出了什么结果?你试过了什么结果? –

+2

[so]是*不*免费代码写入服务。预计你会尝试**自己编写代码**。在[做更多研究]之后(http://meta.stackoverflow.com/questions/261592),如果你有问题,你可以**发布你已经尝试过**的清单,说明什么是不工作的**并提供[**最小,完整和可验证示例**](http://stackoverflow.com/help/mcve)。我建议阅读[问]一个好问题和[完美问题](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/)。另外,一定要参加[旅游]。 – Igor

+3

因为显然,订单很重要('Service3,Service4,Service2'和'Service4,Service2,Serivce3'是不同的结果),那么你在这里指的通常是* Permutations *,而不是* Combinations * (组合类似于排列,但顺序无关紧要)。你可能有更好的运气使用谷歌搜索。 –

回答

1

您可以this library from CodeProject在这个例子中使用的组合类,如:

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); 
} 
+0

这工作完美。谢谢! – Scott

+0

这个答案是错误的,至少OP问它的方式。它将打印出{A B C},{A B D},{A C D}, {B C D},即4选择3 = 4个结果。很明显,订单在这里很重要,因为OP公布了24个结果的清单。你应该使用'Variations'类来代替(在你复制/粘贴的代码的下面)。 –

0

巧合的是,我昨天写了一个程序作为编码挑战finds all permutations of a string with a given length.请注意,我删除重复的内容,例如, “流行”有3个! = 6个字母排列,但'p'重复两次并且不可区分,因此3!/ 2 = 3(即“opp”,“pop”,“ppo”)。

static HashSet<string> outputSet = new HashSet<string>(); 

     // this function will find all the strings of length k you can make from a set of letters N 
     // e.g. "pop" --> pop, ppo, opp 
     static void permuteSetLength(string prefix, string suffix, int length) 
     { 
      if (length == 0) 
      { 
       outputSet.Add(prefix); 
       return; 
      } 

      // use dictionary to remove duplicate prefixes, to avoid permuting the same thing again 
      Dictionary<string, string> newPrefixesAndSuffixes = new Dictionary<string, string>(); 
      // otherwise, calculate our new prefixes by adding each letter of suffix to the prefix, and decrementing length by 1 
      for (int i = 0; i < suffix.Length; i++) 
      { 
       if (!newPrefixesAndSuffixes.ContainsKey(prefix + suffix[i])) // new key 
       { 
        // remove ith character from suffix and add it to prefix 
        permuteSetLength(prefix + suffix[i], suffix.Substring(0,i) + suffix.Substring(i+1), length - 1); 
       } 

      } 
     } 

这应该是一个很好的起点。我相信你可以看到找到字符串(即字符数组)和字符串数组的排列之间的联系。

调用的代码如下所示:

permuteSetLength("", "abcd", 3); 
string[] outputArray = new string[outputSet.Count]; 
outputSet.CopyTo(outputArray); 
Console.WriteLine(String.Join(",", outputArray)); 

它打印出以下几点:

ABC,ABD,ACB,ACD,亚洲开发银行,ADC,BAC,坏,BCA,BCD,BDA ,BDC,驾驶室,CAD,CBA,CBD,CDA,CDB,DAB,DAC,DBA,DBC,DCA,DCB

计数的24,即,4×3×2 = 24,正如预期的。现在应该很容易将每个字符映射到一个字符串,但我建议修改方法本身。

0

这里是另一种解决方案:

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}"); 
} 

也许,代码可以是抛光的多一点,但它作品。

希望这会有所帮助。

+0

你的'Console.WriteLine($“{c.s1} {c.s2} {c.s3}”);'行有问题,因为它不会被编译。 –

+1

这取决于您使用的是哪个版本的c#。 – MaKCbIMKo

+0

啊,我的错。我没有跟上最新的C#版本......我仍然在使用Visual Studio 2010。 :o –