2016-03-30 20 views
0

Sorunum için 6'dan büyük bir sayım var. Bu listeden, tam olarak 6 kart uzunluğunda olan orijinal kartların olası tüm kombinasyonlarını içeren bir liste yapmak istiyorum.Tüm benzersiz konumlar bir listeden nasıl çıkarılır?

yüzden 01,02,03,04,05,06 nesne (bunlar benzersiz olmak zorunda ve sırası önemli değildir) 06,05,04,03,02,01 olarak benim için aynıdır

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

buldum ve kullanılan çözüm:

kamu statik Liste generateAllSubsetCombinations (object [] fullSet, ulong subsetSize) { (fullSet == null) { atmak yeni ArgumentException ("Değer olamaz eğer null. "," fullSet "); } } (subsetSize < 1) { yeni ArgumentException ("Alt küme boyutu 1 veya daha büyük olmalıdır.", "SubsetSize") atarsa; } } if ((ulong) fullSet.LongLength < subsetSize) { yeni ArgumentException atayın ("Alt küme boyutu tam kümedeki toplam giriş sayısından büyük olamaz.", "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

gör 'n' değerlerinden değerler? istatistiklerde 'n'' k' ya da 'nCk' seçer mi? –

+0

Her bir "k" nesnesinin, "n" değerlerinden, lengt n'nin değişken olduğu ve k lengt'in alway'in 6 olduğu her bir "com" nesnesini istiyorum. – schadowfax

cevap

0

Bu seferki benden değil, ama iş yok! Eğer `k tüm kombinasyonlarını oluşturmak istediğiniz Yani

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

Bunun işe yarayacağını söyleyebilirim ama ben bunu test etmedim. Çünkü olası bir alt kümeyi aldığımda kontrol yapmayı çok isterim. Ama giriş için teşekkürler Bir çözüm buldum ve bunu bir gün böyle bir şey ararlarsa bunu diğer insanların yerine gönderdim – schadowfax

İlgili konular