zoukankan      html  css  js  c++  java
  • 输入数组,取组合结果

    /// <summary>
           /// 
           /// </summary>
           /// <param name="arrAllComOut">最后返回的所有组合列表</param>
           /// <param name="arrAllInput">需要进行组合提取的源数组</param>
           /// <param name="comCount">需要今天提取的数量</param>
            public static void GetTheComList(List<int[]> arrAllComOut, List<int> arrAllInput,int comCount)
            {
                try
                {
                    //先清空数据
                    arrAllComOut.Clear();
                    int[] SourceArrIndex = new int[arrAllInput.Count];
                    //int[] SourceArrIndex = new int[15] { 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
                    int[] SourceArrNumber = new int[arrAllInput.Count];
                    //int[] SourceArrNumber = new int[15] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
                    //生成5个数的组合 
                    ProcessSourceData(ref SourceArrIndex, ref SourceArrNumber, arrAllInput, comCount);
                    GetComListByIcount(SourceArrIndex, SourceArrNumber, arrAllComOut, arrAllInput, comCount);
                    //绑定数据到gridview
    
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
            private static void GetComListByIcount(int[] SourceArrIndex, int[] SourceArrNumber, List<int[]> arrAllComIn, List<int> arrAllInput, int ICount)
            {
                while (CheckSourceArrIndex(SourceArrIndex, ICount) == false)
                {
                    int j = 0;
                    int[] Resault = new int[ICount];
                    //一轮处理
                    for (int i = 0; i < SourceArrIndex.Length; i++)
                    {
                        if (SourceArrIndex[i] == 1)
                        {
                            Resault[j] = SourceArrNumber[i];
                            j++;
                        }
                    }
                    arrAllComIn.Add(Resault);
                    //一轮处理结束
                    //处理SourceArrIndex
                    ProcessSourceArrIndex(ref SourceArrIndex);
                }
                //添加最后一条数据,也就是当最后5个为1时的数组
                int[] LastResault = new int[ICount];
                int x = 0;
                for (int c = arrAllInput.Count - 1; c >= arrAllInput.Count - ICount; c--)
                {
                    LastResault[x] = Convert.ToInt16(arrAllInput[c]);
                    x++;
                }
                //int[] LastResault = new int[5] { 11, 12, 13, 14, 15 };
                arrAllComIn.Add(LastResault);
    
            }
            private static 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 static bool CheckSourceArrIndex(int[] SourceArrIndex, int ICount)
            {
                for (int i = 1; i <= ICount; i++)
                {
                    if (SourceArrIndex[SourceArrIndex.Length - i] != 1)
                    {
                        return false;
                    }
                }
             
                return true;
            }
            private static void ProcessSourceData(ref int[] SourceArrIndex, ref int[] SourceArrNumber, List<int> arrInputAll, int ICount)
            {
                int i = 0;
                foreach (int InputInt in arrInputAll)
                {
                    for (int j = 0; j < ICount; j++)
                    {
                        SourceArrIndex[j] = 1;
                    }
                    for (int x = ICount; x < SourceArrIndex.Length; x++)
                    {
                        SourceArrIndex[x] = 0;
                    }
                    SourceArrNumber[i] = InputInt;
                    i++;
                }
            }

    调用方式:

     List<int[]> arrAllCom = new List<int[]>();//需要计算的组合列表
                //得到组合结果集
      Cls_Utility.GetTheComList(arrAllCom, arrInput, comCount);
  • 相关阅读:
    Java环境变量的配置
    Vim的使用
    codeforces round506(div3)A. Many Equal Substrings
    codeforces round 531(div3) D. Balanced Ternary String
    codeforces Manthan, Codefest 18 (rated, Div. 1 + Div. 2) D. Valid BFS
    codeforces codefest18(div1+div2) B. Reach Median
    D. Sum in the tree codeforces round#530(div2)
    codeforces round 508(div2) D. Slime
    codeforces goodbye 2018 C. New Year and the Sphere Transmission D. New Year and the Permutation Concatenation
    C. Classy Numbers cf edu round50
  • 原文地址:https://www.cnblogs.com/tylertang/p/5676707.html
Copyright © 2011-2022 走看看