首页 > 技术文章 > 33选6算法:M个数N个为一组,无重复的排列组合

changweihua 2013-10-10 16:49 原文

        private void button1_Click(object sender, EventArgs e)
        {
            int nCnt = 0;
            List nNumList = new List();
            for (int i = 0; i < cblNumList.Items.Count; i++)
            {
                if (cblNumList.GetItemChecked(i))
                {
                    nNumList.Add(Convert.ToInt32(cblNumList.Items[i].ToString()));
                }
            }
            System.Text.StringBuilder sbResult = new StringBuilder();
            int nLast = nNumList.Count;
            for (int i1 = 0; i1 < nLast-5; i1++)
            {
                for (int i2 = 0; i2 < nLast - 4; i2++)
                {
                    if (i2 == i1) continue;
                    for (int i3 = 0; i3 < nLast - 3; i3++)
                    {
                        if (i3 == i2 || i3 == i1) continue;
                        for (int i4 = 0; i4 < nLast - 2; i4++)
                        {
                            if (i4 == i3 || i4 == i2 || i4 == i1) continue;
                            for (int i5 = 0; i5 < nLast - 1; i5++)
                            {
                                if (i5 == i4 || i5 == i3 || i5 == i2 || i5 == i1) continue;
                                for (int i6 = 0; i6 < nLast; i6++)
                                {
                                    if (i6 == i5 || i6 == i4 || i6 == i3 || i6 == i2 || i6 == i1) continue;
                                    if (nNumList[i1] > nNumList[i2] || nNumList[i2] > nNumList[i3] || nNumList[i3] > nNumList[i4] || nNumList[i4] > nNumList[i5] || nNumList[i5] > nNumList[i6]) continue;
                                    sbResult.Append("\r\n" + nNumList[i1].ToString() + " " + nNumList[i2].ToString() + " " + nNumList[i3].ToString() + " " + nNumList[i4].ToString() + " " + nNumList[i5].ToString() + " " + nNumList[i6].ToString());
                                    nCnt += 1;
                                }
                            }
                        }
                    }
                }
            }
            tbResult.Text = sbResult.ToString();
            //lblMessage.Text = nLast + " 个随机数字;共有 " + nCnt + " 条记录";
        }

推荐阅读