2016-03-30 6 views
0

私の問題では、6以上の数のリストがあります。そのリストから、ちょうど6枚のカードである元のカードのすべての可能な組み合わせを含むリストを作成したいと思います。 (彼らは関係ないユニークかつ順序である必要はあり)一意性をどのようにリストから外すか?

ので 01,02,03,04,05,06 が 06,05,04,03,02,01のように私のために同じであるオブジェクト

//STARTER list with more then 6 value's 
List <ClassicCard> lowCardsToRemove = FrenchTarotUtil.checkCountLowCardForDiscardChien(handCards); 

iが見出され、使用溶液:

パブリック静的リストgenerateAllSubsetCombinations(オブジェクト[]フルセット、ULONG subsetSize){ 場合(フルセット== NULL){ スロー新しいArgumentExceptionが( "値はできませんnull "、" fullSet "); } else if(subsetSize <){ throw new ArgumentException( "サブセットサイズは1以上でなければなりません。"、 "subsetSize"); } else if((ulong)fullSet.LongLength < subsetSize){ throw new ArgumentException( "サブセットのサイズは、フルセット内のエントリの総数より大きくすることはできません。"、 "subsetSize"); }

// All possible subsets will be stored here 
    List<object[]> allSubsets = new List<object[]>(); 

    // Initialize current pick; will always be the leftmost consecutive x where x is subset size 
    ulong[] currentPick = new ulong[subsetSize]; 
    for (ulong i = 0; i < subsetSize; i++) { 
     currentPick[i] = i; 
    } 

    while (true) { 
     // Add this subset's values to list of all subsets based on current pick 
     object[] subset = new object[subsetSize]; 
     for (ulong i = 0; i < subsetSize; i++) { 
      subset[i] = fullSet[currentPick[i]]; 
     } 
     allSubsets.Add(subset); 

     if (currentPick[0] + subsetSize >= (ulong)fullSet.LongLength) { 
      // Last pick must have been the final 3; end of subset generation 
      break; 
     } 

     // Update current pick for next subset 
     ulong shiftAfter = (ulong)currentPick.LongLength - 1; 
     bool loop; 
     do { 
      loop = false; 

      // Move current picker right 
      currentPick[shiftAfter]++; 

      // If we've gotten to the end of the full set, move left one picker 
      if (currentPick[shiftAfter] > (ulong)fullSet.LongLength - (subsetSize - shiftAfter)) { 
       if (shiftAfter > 0) { 
        shiftAfter--; 
        loop = true; 
       } 
      } 
      else { 
       // Update pickers to be consecutive 
       for (ulong i = shiftAfter+1; i < (ulong)currentPick.LongLength; i++) { 
        currentPick[i] = currentPick[i-1] + 1; 
       } 
      } 
     } while (loop); 
    } 

    return allSubsets; 
} 
+2

はhttp://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n –

+0

を参照してください。 'n値からの値? 'n'のように統計で' k'または 'nCk'を選ぶ? –

+0

lengt nが可変で、k lengtが常に6である "n"値から "k"個のオブジェクトのすべてのユニークなコンボを欲しい。 – schadowfax

答えて

0

この1つは私のものではありませんが、それは仕事です!あなたは `kのすべての組み合わせを生成したいので

List <ClassicCard> lowCardsToRemove = FrenchTarotUtil.checkCountLowCardForDiscardChien(handCards); 
var result = Combinator.Combinations(lowCardsToRemove, 6); 

public static class Combinator 
{ 
    public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<T> elements, int k) 
    { 
     return k == 0 ? new[] { new T[0] } : 
      elements.SelectMany((e, i) => 
      elements.Skip(i + 1).Combinations(k - 1).Select(c => (new[] { e }).Concat(c))); 
    } 
} 
+0

これは動作する可能性がありますが、テストしなかった可能性があります。私は可能なサブセットを取得するときにチェックをするのが大好きなので。しかし、入力のおかげで 私は解決策を見つけました、そして、私はそれがこの一日のようなものを探すならば、他の人々のためにこれをundernetで投稿します – schadowfax

関連する問題