我在网上找了很多关于组合的代码?
看起来就晕,或者没有找到符合我的意思的学习资料
我需要例如:从01 02 到11这11个数中任意选取5个、4个、3个或者2个数的组合,不需要大小顺序、位顺序,请大家帮忙写一个学习代码,并请详细注明注释(小虾是一个刚刚学习c#的新手)

解决方案 »

  1.   

    http://topic.csdn.net/u/20110922/14/f5e06973-85fd-4a08-8ac9-a3f690273de3.html
      

  2.   


                Random Rnd = new Random();
                string rel = "";
                List<int> num = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
                for (int i = 0; i < Rnd.Next(1, num.Count); i++)
                {
                    Random Rnd1 = new Random();
                    int j = Rnd1.Next(0, num.Count);
                    rel += num[j].ToString() + ",";
                    num.RemoveAt(j);
                }
                Console.WriteLine("{0}",rel);
      

  3.   

    终于弄明白了,不过感觉不美,请各位大侠看看是不是还能优化一下代码namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                string[] str={"01","02","03","04","05","06","07","08","09","10","11"};
                string a1, a2, a3, a4, a5;
                for (int i = 0; i < str.Length - 1; i++)
                {
                    a1 = str[i];
                    for (int j = i + 1; j < str.Length; j++)
                    {
                        a2 = str[j];
                        listBox2.Items.Add(a1+" "+a2);
                        for (int n = j + 1; n < str.Length; n++)
                        {
                            a3 = str[n];
                            listBox3.Items.Add(a1+" "+a2+" "+a3);
                            for (int m = n + 1; m < str.Length; m++)
                            {
                                a4 = str[m];
                                listBox4.Items.Add(a1+" "+a2+" "+a3+" "+a4);
                                for (int k = m + 1; k < str.Length; k++)
                                {
                                    a5 = str[k];
                                    listBox1.Items.Add(a1 + " " + a2 + " " + a3 + " " + a4 + " " + a5);
                                }
                            }
                        }
                    }
                }            label3.Text = listBox1.Items.Count.ToString();
                label4.Text = listBox2.Items.Count.ToString();
                label5.Text = listBox3.Items.Count.ToString();
                label6.Text = listBox4.Items.Count.ToString();
            }
        }
    }