Skip to main content
Code Review

Return to Answer

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

This is a base 3 number, of 5 digits (with the extra note that each digit must have +1 to it and total for all digits must equal 5). I based this on my own SO answer here: http://stackoverflow.com/a/9315076/360211 https://stackoverflow.com/a/9315076/360211

This is a base 3 number, of 5 digits (with the extra note that each digit must have +1 to it and total for all digits must equal 5). I based this on my own SO answer here: http://stackoverflow.com/a/9315076/360211

This is a base 3 number, of 5 digits (with the extra note that each digit must have +1 to it and total for all digits must equal 5). I based this on my own SO answer here: https://stackoverflow.com/a/9315076/360211

Source Link
weston
  • 186
  • 6

This is a base 3 number, of 5 digits (with the extra note that each digit must have +1 to it and total for all digits must equal 5). I based this on my own SO answer here: http://stackoverflow.com/a/9315076/360211

class Program
 {
 static void Main()
 {
 int balls = 5;
 int buckets = 3;
 int maxInBucket = balls - buckets + 1; //because of rule about must be 1
 int baseN = maxInBucket;
 int maxDigits = buckets;
 var max = Math.Pow(baseN, maxDigits);
 for (int i = 0; i < max; i++)
 { // each iteration of this loop is another unique permutation 
 var digits = new int[maxDigits];
 int value = i;
 int place = digits.Length - 1;
 while (value > 0)
 {
 int thisdigit = value % baseN;
 value /= baseN;
 digits[place--] = thisdigit;
 }
 var totalBallsInThisCombo = digits.Sum(d => d + 1); //+1 because each bucket always has one in it
 if (totalBallsInThisCombo != balls) //this is the implementation of rule about must = 5
 {
 continue;
 }
 Console.Write("Balls: ");
 foreach (var digit in digits)
 {
 Console.Write(digit + 1); //+1 because each bucket always has one in it
 Console.Write(" ");
 }
 Console.WriteLine("| Total = {0}", totalBallsInThisCombo);
 }
 Console.ReadLine();
 }
 }
lang-cs

AltStyle によって変換されたページ (->オリジナル) /