c# - how to go through all possible combinations for filling an array or set of variables -


this kind of questions may have been answered, can't go past process of conception small programm trying make in c#.

what want :

-i have n = 100

-i have 9 variables/an array of 9

-i want possible combinations of distributing n between 9 variables, :

v1 = 100; v2 = 0; v3 = 0; v4 = 0; v5 = 0; v6 = 0; v7 = 0; v8 = 0; v9 = 0; 

or

v1 = 10; v2 = 5; v3 = 13; v4 = 27; v5 = 0; v6 = 34; v7 = 0; v8 = 11; v9 = 0; 

i have looked @ heap's algorythm, not seem fit, or may not understand full concept.

also, method calculate number of combinations, product method right 1 ?

edit : found number of possible combinations using online tool found continuation. total of these "352 025 629 371". yeah take while go through of these.

this may started. brute forces of combinations.

using system; using system.collections.generic; using system.linq;  namespace bob {     public class program     {         private static ienumerable<combination> getdata()         {             (int = 0; < 101; a++)             {                 (int b = 0; b < 101; b++)                 {                     (int c = 0; c < 101; c++)                     {                         (int d = 0; d < 101; d++)                         {                             (int e = 0; e < 101; e++)                             {                                 (int f = 0; f < 101; f++)                                 {                                     (int g = 0; g < 101; g++)                                     {                                         (int h = 0; h < 101; h++)                                         {                                             (int = 0; < 101; i++)                                             {                                                 var sum = + b + c + d + e + f + g + h + i;                                                  if (sum == 100)                                                 {                                                     yield return new combination(a, b, c, d, e, f, g, h, i);                                                 }                                                 else if (sum > 100)                                                 {                                                     break;                                                 }                                             }                                         }                                     }                                 }                             }                         }                     }                 }             }         }          static void main(string[] args)         {             var sampleresults = getdata().take(200); // show 200 (remove .take(100) show them all)              foreach (var result in sampleresults)             {                 console.writeline(result);             }             console.readline();         }     }      public class combination     {         public readonly int v1;         public readonly int v2;         public readonly int v3;         public readonly int v4;         public readonly int v5;         public readonly int v6;         public readonly int v7;         public readonly int v8;         public readonly int v9;          public combination(int v1, int v2, int v3, int v4, int v5, int v6, int v7, int v8, int v9)         {             v1 = v1;             v2 = v2;             v3 = v3;             v4 = v4;             v5 = v5;             v6 = v6;             v7 = v7;             v8 = v8;             v9 = v9;         }          public override string tostring()         {             return string.format("{0} {1} {2} {3} {4} {5} {6} {7} {8}", v1, v2, v3, v4, v5, v6, v7, v8, v9);         }     } } 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -