Get combines by recursion, low performance.
class Program
{
/// <summary>
/// Print all combines of C(n)(Left)
/// </summary>
/// <param name="left">which is not used</param>
/// <param name="result">the results</param>
/// <param name="n">combine number</param>
private void Combine(ref string left, ref string result, int n)
{
if (left == null)
{
throw new ArgumentNullException("left");
}
if (result == null)
{
throw new ArgumentNullException("result");
}
if (n < 0)
{
throw new ArgumentNullException("n");
}
if (0 == n)
{
Console.Write("(");
for (int i = 0; i < result.Length; i++)
{
Console.Write(result[i]);
if (i < result.Length -1)
{
Console.Write(",");
}
}
Console.Write(") ");
}
else if (n <= left.Length)
{
// Retrieve the data
result += left[0];
string tmp_left = left.Substring(1);
// Try to get the left n-1 data
// a-> ab-> abc
Combine(ref tmp_left, ref result, n - 1);
// Pop the corrupted data
// abc-> ab
result = result.Substring(0, result.Length - 1);
// Re-get data from left candidates
// ab-> ab? (?=defg)
Combine(ref tmp_left, ref result, n);
}
else
{
return;
}
}
static void TestCase1()
{
// string WORDS = "abcdefghijklmnopqrstuvwxyz";
string WORDS = "abcdefg";
Program p = new Program();
string result = string.Empty;
for (int i = 1; i <= WORDS.Length; i++)
{
try
{
p.Combine(ref WORDS, ref result, i);
Console.WriteLine();
//Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
static void Main(string[] args)
{
TestCase1();
}
}