2017-03-16 51 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 –