2016-07-21 7 views
1

현재 내가 원하는 것을하고있는이 코드 스 니펫이 있습니다. 일부 개체의 경우에는 가능한 모든 조합이 만들어집니다.combinatory 하드 코드 스 니펫을 재귀 함수로 변환하는 방법은 무엇입니까?

이 예에서는 4 개의 객체 (0, 1, 2 및 3)가 있다고 가정하고 4 개의 객체의 가능한 모든 조합이 만들어집니다 (0, 1, 2, 3, 01), 02, 03, 12, 13, 23, 012, 013, 023, 123 및 0123).

2^4 - 1 = 15 조합과 일반적으로 2^객체 수 - 1 조합이 있어야합니다.

이 코드로 생성 된 개체의 순서는 다음과 같습니다. 0 -> 01 -> 012 ->-> 013 -> 02 -> 023 -> 03 -> 1 -> 12 -> 123 -> 13 - > 2 -> 23 -> 3

초기 객체를 얻는 방법과 그 수는 코드의 다른 곳에서 정의됩니다. 또한 최대 결정 오브젝트 수 (함께 오른쪽의 결과를 제공에도 불구

int count = 4; //this is gotten elsewhere 
int currPos = 0; 
var objects = new Object[(2^count)-1]; 

for (int i = 0; i < count; i++) //loop that creates combinations of only one object 
{ 
    Object obj = new Object(...); 
    objects[currPos] = obj; 
    currPos += 1; 

    for (int j = i + 1; j < count; j++) //loop that creates combinations of two objects 
    { 
     Object obj = new Object(...); 
     objects[currPos] = obj; 
     currPos += 1; 

     for (int k = j + 1; k < count; k++) //loop that creates combinations of three objects 
     { 
      Object obj = new Object(...); 
      objects[currPos] = obj; 
      currPos += 1; 

      for (int l = k + 1; l < count; l++) //loop that creates combinations of four objects 
      { 
       Object obj = new Object(...); 
       objects[currPos] = obj; 
       currPos += 1; 
      } 
     } 
    } 
} 

이 하드 코딩되어 있으므로 I 재귀 함수로 변경하는 방법을보고하고있다 (단, 그 기능을 maintainig) 조합, 예에서 4 개)를 매개 변수로 전달합니다. 내가 필요한 경우는 "이전"루프에 갈 수가 없어 주로하기 때문에 나는 아래의 코드처럼하지만 결과에 뭔가를 시도하고있다

013

int count = 4; 
    int currPos = 0; 
    var objects = new Object[(2^count)-1]; 
    combinations(count, 0, currPos, objects); //called elsewhere 

    private void combinations(int numberOfObjects, int j, int count, int currPos, Object[] objects) 

    { 
     if (numberOfObjects == count) 
     { 
      for (int k = j; k < count; k++) 
      { 
       Object obj = new Object(...); 
       objects[currPos] = obj; 
       currPos += 1; 
       generateCombinations(numberOfObjects - 1, j + 1, count, currPos, objects); 
      } 
     } 

     if (numberOfObjects < count) 
     { 
      for (int l = j; l < count; l++) 
      { 
       Object obj = new Object(...); 
       objects[currPos] = obj; 
       currPos += 1; 

       (...) 

       generateCombinations(..., ..., count, currPos, objects); 
      } 
     } 
    } 
에 0123에서가는 예
+0

놀라운 그 [검색] (https://www.bing.com/search를 ? q = c % 23 % 20all % 20combinations) 기존 솔루션을 찾지 못했습니다 ... [this] (http://stackoverflow.com/questions/774457/combination-generator-in-linq)와 같이 –

+0

'generateCombinations' 다른 기능 또는 맞춤법 오류? – Groo

답변

0

이것은 너가하는 일 이니?

var combinations = GetCombinations(new[] { "0", "1", "2", }); 

그리고이 결과를 얻을 :

public IEnumerable<string> GetCombinations(IEnumerable<string> source) 
{ 
    if (source == null || !source.Any()) 
    { 
     return Enumerable.Empty<string>(); 
    } 
    else if (source.Skip(1).Any()) 
    { 
     return new string[] { null, source.First() }.SelectMany(x => GetCombinations(source.Skip(1)), (x, y) => x + y); 
    } 
    else 
    { 
     return new string[] { null, source.First() }; 
    } 
} 

I는 다음과 같이 사용할 수 있습니다

 
null 
2 
1 
12 
0 
02 
01 
012