Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would prefer Next permutation method. Or you could use brute force with 6 nested for loops which is more simple to implement.</p> <p>Here's a C# version of Next permutation converted from ChengYi He's post</p> <pre><code>using System; using System.Collections.Generic; using System.Linq; static class Extensions { public static string GetString(this IEnumerable&lt;int&gt; arr) { return arr.Aggregate("", (a, b) =&gt; a + " " + b); } } class Program { const int Sum = 21; const int Len = 6; static void Main(string[] args) { var result = new List&lt;string&gt;(); Run(result); result.ForEach(Console.WriteLine); Console.WriteLine(string.Format("N: {0}\nPress a key to exit", result.Count)); Console.ReadKey(); } static void Run(IList&lt;string&gt; result) { var n = new int[Len]; n[0] = Sum; result.Add(n.GetString()); while (FindNext(n)) { result.Add(n.GetString()); } } static bool FindNext(IList&lt;int&gt; perm) { if (perm.Last() == Sum) { return false; } int i; for (i = 0; i &lt; Len; i++) { if (perm[i] != 0) { break; } } perm[i + 1]++; perm[i]--; if (i &gt; 0) { int tmp = perm[i]; perm[i] = perm[0]; perm[0] = tmp; } return true; } } </code></pre> <p>The brute force version would be like this in C#</p> <pre><code>static void Main(string[] args) { var result = new List&lt;string&gt;(); for (int a = 0; a &lt;= Sum; a++) for (int b = 0; b &lt;= Sum; b++) for (int c = 0; c &lt;= Sum; c++) for (int d = 0; d &lt;= Sum; d++) for (int e = 0; e &lt;= Sum; e++) for (int f = 0; f &lt;= Sum; f++) if(a+b+c+d+e+f==Sum) result.Add(string.Format( "{0} {1} {2} {3} {4} {5}", a,b,c,d,e,f)); result.ForEach(Console.WriteLine); Console.WriteLine(string.Format("N: {0}\nPress a key to exit", result.Count)); Console.ReadKey(); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload