日期:2014-05-18  浏览次数:20862 次

排列组合算法
如何计算得到: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())
{
...
}
}
------解决方案--------------------
C# code
for(int i=1;i<=9;i++)
 for(int j=i+1;j<=9;j++)
    Console.WriteLine("{0},{1}",i,j);

------解决方案--------------------

C# code


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()]);
                }
            }