今天搞懂了求子集的二进制算法,网上大多数都是用java来实现的,找了半天,也没找到C#版本的,进过仔细研究,终于OK了
static void SubSet()
{
char[] chs = { 'a', 'b', 'c', 'd' };
int len = chs.Length;
for (int i = 0; i < Math.Pow(2, len); i++)
{
String str = Convert.ToString(i, 2);
String str1 = str.PadLeft(len, '0');
try
{
int toBinary = Int32.Parse(str1);
}
catch (FormatException e)
{
Console.WriteLine(e.Message);
}
char[] toCharArray = str1.ToCharArray(); ;
System.Console.Write("{");
for (int j = 0; j < toCharArray.Length; j++)
{
if (toCharArray[j] == '1')
System.Console.Write(chs[j] + ",");
}
System.Console.Write("}");
} Console.ReadLine();
}