组合数获取算法

跟据提供的不重复数组,获取其中包含的组合数:

 /*
          组合算法   
          本程序的思路是开一个数组,其下标表示1到m个数,数组元素的值为1表示其下标   
          代表的数被选中,为0则没选中。     
          首先初始化,将数组前n个元素置1,表示第一个组合为前n个数。     
          然后从左到右扫描数组元素值的“10”组合,找到第一个“10”组合后将其变为   
          “01”组合,同时将其左边的所有“1”全部移动到数组的最左端。     
          当第一个“1”移动到数组的m-n的位置,即n个“1”全部移动到最右端时,就得   
          到了最后一个组合。     
          例如求5中选3的组合:     
          1   1   1   0   0   //1,2,3     
          1   1   0   1   0   //1,2,4     
          1   0   1   1   0   //1,3,4     
          0   1   1   1   0   //2,3,4     
          1   1   0   0   1   //1,2,5     
          1   0   1   0   1   //1,3,5     
          0   1   1   0   1   //2,3,5     
          1   0   0   1   1   //1,4,5     
          0   1   0   1   1   //2,4,5     
          0   0   1   1   1   //3,4,5   
         */
        ArrayList ResaultAll = new ArrayList();
        int SXcount = 0;
        int SWCZ = 0;
        private void btn_GetAll_Click(object sender, EventArgs e)
        {
            try
            {
                int[] SourceArrIndex = new int[15] { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                int[] SourceArrNumber = new int[15] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };


                //先清空数据
                this.dataGridView1.Rows.Clear();
                ResaultAll.Clear();
                this.textBox1.Text = "";
                while (CheckSourceArrIndex(SourceArrIndex) == false)
                {
                    int j = 0;
                    int[] Resault = new int[5];
                    //一轮处理
                    for (int i = 0; i < SourceArrIndex.Length; i++)
                    {
                        if (SourceArrIndex[i] == 1)
                        {
                            Resault[j] = SourceArrNumber[i];
                            j++;
                        }
                    }
                    ResaultAll.Add(Resault);
                    //一轮处理结束
                    //处理SourceArrIndex
                    ProcessSourceArrIndex(ref SourceArrIndex);
                }
                //添加最后一条数据,也就是当最后5个为1时的数组
                int[] LastResault = new int[5] { 11, 12, 13, 14, 15 };
                ResaultAll.Add(LastResault);

                //绑定数据到gridview
                BindData(ResaultAll);
                textBox2.Text = ResaultAll.Count.ToString();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message );
            }
        }
        private void ProcessSourceArrIndex(ref int[] SourceArrIndex)
        {
            try
            {
                int x = 0;
                for (int i = 0; i < SourceArrIndex.Length; i++)
                {
                    if (i != SourceArrIndex.Length - 1 && SourceArrIndex[i] == 1)
                    {
                        //只处理第一个10
                        if (SourceArrIndex[i + 1] == 0)
                        {
                            //交换
                            SourceArrIndex[i] = 0;
                            SourceArrIndex[i + 1] = 1;
                            //左移
                            for (int j = 0; j <= i; j++)
                            {
                                if (SourceArrIndex[j] == 1)
                                {
                                    //如果为1, 则设置为0,并且将前面的第X个元素设置为1
                                    SourceArrIndex[j] = 0;
                                    SourceArrIndex[x] = 1;
                                    x++;
                                }
                            }
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        private bool CheckSourceArrIndex(int[] SourceArrIndex)
        {
            if (SourceArrIndex[SourceArrIndex.Length - 1] == 1 && SourceArrIndex[SourceArrIndex.Length - 2] == 1 && SourceArrIndex[SourceArrIndex.Length - 3] == 1 && SourceArrIndex[SourceArrIndex.Length - 4] == 1 && SourceArrIndex[SourceArrIndex.Length - 5] == 1)
            {
                return true;
            }
            return false;
        }

 

posted @ 2013-07-25 14:51  龍☆  阅读(380)  评论(0编辑  收藏  举报