排列组合算法
如何计算得到:0~9十个数字选2的 45种排列组合
[解决办法]
IEnumerable<int[]> foo()
{
for (int i = 0; i < 10; i++)
for (int j = i + 1; j < 10; j++)
yield return new int[] { i, j };
}
void Main()
{
foreach (var item in foo())
{
...
}
}
[解决办法]
for(int i=1;i<=9;i++) for(int j=i+1;j<=9;j++) Console.WriteLine("{0},{1}",i,j);
[解决办法]
public static List<string> P(string s) { if (s.Length==1) { List<string> r = new List<string>(); r.Add(""); r.Add("a"); return r; } else { List<string> tmp = P(s.Substring(0, s.Length - 1)); List<string> ret=new List<string>(); foreach (string t in tmp) { ret.Add(t); } foreach (string t in tmp) { for (int len = t.Length; len >=0; len--) { string insert = t; insert=insert.Insert(len, s[s.Length - 1].ToString()); ret.Add(insert); } } return ret; } }List<string> ret = P("abcdefghij"); Dictionary<int, string> dic = new Dictionary<int, string>(); foreach (string s in ret) { try { dic.Add(s.GetHashCode(), s); } catch { MessageBox.Show(s + "_" + dic[s.GetHashCode()]); } }