private int SortCount(int N) { int Count = 0; for (int i = 1; i <= N; i++) { Count += C(i, N); } return Count; } int C(int n, int N) { int result = 1; for (int i = 0; i < n; i++) { result *= N - i; } result = result / JieChen(n); return result; } int JieChen(int n) { if (n == 0 || n == 1) return 1; else return JieChen(n - 1) * n; } int count = SortCount(3); //count=7
static string[] m_Data = { "A", "B", "C", "D", "E" }; static void Main(string[] args) { Dictionary<string, int> dic = new Dictionary<string, int>(); for (int i = 0; i < m_Data.Length; i++) { Console.WriteLine(m_Data[i]);//如果不需要打印单元素的组合,将此句注释掉 dic.Add(m_Data[i], i); } GetString(dic); Console.ReadLine(); } static void GetString(Dictionary<string,int> dd) { Dictionary<string, int> dic = new Dictionary<string, int>(); foreach (KeyValuePair<string, int> kv in dd) { for (int i = kv.Value + 1; i < m_Data.Length; i++) { Console.WriteLine(kv.Key + m_Data[i]); dic.Add(kv.Key + m_Data[i], i); } } if(dic.Count>0) GetString(dic); } 这里输出是正确的,来源#17
高效率的全组合算法 static string[] m_Data = { "A", "B", "C", "D", "E" }; static void Main(string[] args) { Dictionary<string, int> dic = new Dictionary<string, int>(); for (int i = 0; i < m_Data.Length; i++) { Console.WriteLine(m_Data[i]);//如果不需要打印单元素的组合,将此句注释掉 dic.Add(m_Data[i], i); } GetString(dic); Console.ReadLine(); } static void GetString(Dictionary<string,int> dd) { Dictionary<string, int> dic = new Dictionary<string, int>(); foreach (KeyValuePair<string, int> kv in dd) { for (int i = kv.Value + 1; i < m_Data.Length; i++) { Console.WriteLine(kv.Key + m_Data[i]); dic.Add(kv.Key + m_Data[i], i); } } if(dic.Count>0) GetString(dic); }
{
int length = n.length;
if(n == 0)
return 0;
else
return Num(length);
}
int Num(int n)
{
if(n == 1)
return n;
if(n > 1)
return n*Num(n-1);
}
你写的是返回组合的个数,我需要的是返回这些组合,放在list里面
如果是我排列,我会从{A,B,C} {AB,AC,BC} {ABC} 获得这7个组合。你的这样写出来的顺序我没想出来是怎么排列的。。
看完我写的A,B,C的组合 = {A,B,AB} + {C} + {AC,BC,ABC} = {A,B的组合} + {C} + C{A,B的组合}看第二个等号后面的这样就把(A,B,C)三个字母的排列,分解成两个字母的排列结果的方式不就可以用递归解决了么?多想想
think for a while
public String[] GetAllCombination(String str,Int32 n) //n < str.Length
{
Int32[] state = new Int32[str.Length];
Char[] chars = str.ToCharArray();
List<String> list = new List<String>(); for (Int32 i = 0; i < n; i++)
{
state[i] = 1;
}
for (Int32 i = n; i < state.Length; i++)
{
state[i] = 0;
} list.Add(str.Substring(0,n));
while (true)
{
Boolean flag = true;
Int32 index = 0;
Int32 oneCount = 0;
Int32 zeroCount = 0;
StringBuilder sbuilder = new StringBuilder(); for(; index < state.Length - 1; index++)
{
if (state[index] == 1 && state[index + 1] == 0)
{
Int32 temp = state[index];
state[index] = state[index + 1];
state[index + 1] = temp;
break;
}
} for (Int32 i = 0; i < index; i++)
{
if (state[i] == 0)
zeroCount++;
else
oneCount++;
} for (Int32 i = 0; i < index; i++)
{
if (i < oneCount)
state[i] = 1;
else
state[i] = 0;
} for (Int32 i = 0; i < chars.Length; i++)
{
if (state[i] == 1)
sbuilder.Append(chars[i]);
} list.Add(sbuilder.ToString()); for (Int32 i = 0; i < chars.Length - n; i++)
{
if (state[i] == 1)
{
flag = false;
break;
}
}
if (flag)
break;
} return list.ToArray();
}方法输入为一个字符串,但和你的有区别,不含有逗号
对于有逗号的可以用
String str = "A,B,C,D";
str = str.Replace(",","");
处理,然后调用该方法//from my blog
http://www.cnblogs.com/Peter-Zhang/articles/1783814.html
http://topic.csdn.net/u/20090217/21/F41ED9F6-F929-451C-A5C9-80D2E408422A.html
private int SortCount(int N)
{
int Count = 0;
for (int i = 1; i <= N; i++)
{
Count += C(i, N);
}
return Count;
} int C(int n, int N)
{
int result = 1;
for (int i = 0; i < n; i++)
{
result *= N - i;
}
result = result / JieChen(n);
return result;
}
int JieChen(int n)
{
if (n == 0 || n == 1)
return 1;
else
return JieChen(n - 1) * n;
}
int count = SortCount(3); //count=7
{
Dictionary<string, int> dic = new Dictionary<string, int>();
for (int i = 0; i < m_Data.Length; i++)
{
Console.WriteLine(m_Data[i]);//如果不需要打印单元素的组合,将此句注释掉
dic.Add(m_Data[i], i);
}
GetString(dic);
Console.ReadLine();
} static void GetString(Dictionary<string,int> dd)
{
Dictionary<string, int> dic = new Dictionary<string, int>();
foreach (KeyValuePair<string, int> kv in dd)
{
for (int i = kv.Value + 1; i < m_Data.Length; i++)
{
Console.WriteLine(kv.Key + m_Data[i]);
dic.Add(kv.Key + m_Data[i], i);
}
}
if(dic.Count>0) GetString(dic);
}
这里输出是正确的,来源#17
高效率的全组合算法
static string[] m_Data = { "A", "B", "C", "D", "E" }; static void Main(string[] args)
{
Dictionary<string, int> dic = new Dictionary<string, int>();
for (int i = 0; i < m_Data.Length; i++)
{
Console.WriteLine(m_Data[i]);//如果不需要打印单元素的组合,将此句注释掉
dic.Add(m_Data[i], i);
}
GetString(dic);
Console.ReadLine();
} static void GetString(Dictionary<string,int> dd)
{
Dictionary<string, int> dic = new Dictionary<string, int>();
foreach (KeyValuePair<string, int> kv in dd)
{
for (int i = kv.Value + 1; i < m_Data.Length; i++)
{
Console.WriteLine(kv.Key + m_Data[i]);
dic.Add(kv.Key + m_Data[i], i);
}
}
if(dic.Count>0) GetString(dic);
}