Generate list of all possible combinations
In C#, you may encounter situations where you need to process a list of integers but don't know the number of items until runtime. To solve this situation, it is crucial to find all possible combinations of lists.
To do this, you can utilize a mathematical method:
<code class="language-csharp">static void GetCombination(List<int> list) { double count = Math.Pow(2, list.Count); for (int i = 1; i < count; i++) { string str = Convert.ToString(i, 2); str = str.PadLeft(list.Count, '0'); Console.Write("{"); for (int j = 0; j < str.Length; j++) { if (str[j] == '1') { Console.Write(list[j] + ","); } } Console.WriteLine("}"); } }</code>
This method:
The above is the detailed content of How to Generate All Possible Combinations of a List in C#?. For more information, please follow other related articles on the PHP Chinese website!