私の問題では、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;
}
はhttp://stackoverflow.com/questions/127704/algorithm-to-return-all-combinations-of-k-elements-from-n –
を参照してください。 'n値からの値? 'n'のように統計で' k'または 'nCk'を選ぶ? –
lengt nが可変で、k lengtが常に6である "n"値から "k"個のオブジェクトのすべてのユニークなコンボを欲しい。 – schadowfax