zoukankan      html  css  js  c++  java
  • 组合数获取算法

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

     /*
              组合算法   
              本程序的思路是开一个数组,其下标表示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;
            }
  • 相关阅读:
    000 初步使用Kotlin开发Android应用
    使用Kotlin开发Android应用
    使用Kotlin开发Android应用(IV):自定义视图和Android扩展
    使用Kotlin开发Android应用(III):扩展函数和默认值
    使用Kotlin开发Android应用(II):创建新工程
    使用Kotlin开发Android应用
    Retrofit2.2说明-简单使用
    Log图文详解(Log.v,Log.d,Log.i,Log.w,Log.e)
    android开发环境 eclipse + android sdk配置笔记
    Android 百度地图定位(手动+自动) 安卓开发教程
  • 原文地址:https://www.cnblogs.com/tylertang/p/3214364.html
Copyright © 2011-2022 走看看