随机数组 5 6 7 8 9 10 11 12 13 14 15 16
1 1 1 1 1 1 1 1 1 1x2 无 1x2
1 1 1 1 1 1 1 1 1x2 解 3x2
1 1 1 1 1 1x2
3 3 3 3 3 3 3 3 3
3 3 3 3 3 3 3
4 4 4 4 4 4 4x2 4x2
如上表所示,第一列为随机数组,第二列开始第一行为指定的数字,第二列开始第二行往下为该指定数字下,符合该数量的最多元素组合,如何使用编程的方式得出从左边第二列开始的结果呢?百度也提问了,知道的一并结贴,谢谢。
http://zhidao.baidu.com/question/386291385.html?quesup2&oldq=1

解决方案 »

  1.   

    随机数组 5  6  7  8  9 10  11  12  13  14  15  16  17...
        1    1  1  1  1  1     1   1   1   1x2 无  1x2 
        1  1  1  1  1  1         1   1 1x2 解  3x2 
        1     1  1     1     1 1x2     
        3  3  3   3  3   3  3   3   3
        3   3  3   3  3   3   3
        4        4  4  4 4   4 4x2 4x2  ...重新排列了一下数字,望多指教- -
      

  2.   

    因为自动处理多余的空格,这个网站无法排出需要显示的结果,百度的链接可以看出来,我再试试,谢谢。随机数组 05 06 07 08 09 10 11 12 13 14  15 16
    ------01 01 01 01 01 01 00 01 01 01 1x2 00 1x2
    ------01 01 01 01 00 01 00 00 01 01 1x2 00 00
    ------01 00 01 01 00 01 00 00 00 01 1x2 00 00
    ------03 03 03 00 03 03 03 03 03 03 00  00 3x2
    ------03 00 00 00 00 03 03 03 03 03 00 00 00
    ------04 00 00 04 01 00 04 04 04 04 4x2 00 4x2其中00代表不取这个元素。即随机数组{1,1,3,3,4} 如果按5的组合为{1,1,3}加起来为5,按16为{1,3,4}x2 加起来为16,不知我这样解释能否看得明白。
      

  3.   

    不好意思,上文错误太多,修正如下:随机数组 05 06 07 08 09 10 11 12 13 14 15 16
    ------01 01 01 01 01 01 00 01 01 01 1x2 00 1x2
    ------01 01 01 01 00 01 00 00 01 01 1x2 00 00
    ------01 00 01 01 00 01 00 00 00 01 1x2 00 00
    ------03 03 03 00 03 03 03 03 03 03 00 00  3x2
    ------03 00 00 00 00 03 03 03 03 03 00 00 00
    ------04 00 00 04 04 00 04 04 04 04 4x2 00 4x2
    其中00代表不取这个元素。即随机数组{1,1,1,3,3,4} 如果按5的组合为{1,1,3}加起来为5,按16为{1,3,4}x2 加起来为16,不知我这样解释能否看得明白。
      

  4.   

                int S = 5;// 随机数
                List<int> columnIndex = new List<int>();//与随机数相等的列序号list
                string[,] fn = new string[10, 20];//10 rows 20 columns
                for (int i = 0; i < 20; i++)
                {
                    int temp = 0;
                    for (int y = 0; y < 10; y++)
                    {
                        if (fn[y, i].ToString().Contains("x"))
                        {
                            string[] tem = fn[y, i].Split('x');
                            temp += int.Parse(tem[0]) * int.Parse(tem[1]);
                        }
                        else
                        {
                            temp+=int.Parse(fn[y, i].ToString());
                        }
                    }
                    if (temp == S)
                    {
                        columnIndex.Add(i);
                    }
                }
      

  5.   

    to : BenBenCode 看不懂是我没表达明白,再试试如下。
    前提条件是:
    1,一个条随机数组。如{1,1,1,3,4}或者{1,1,2,2,3,5}等等只有一个数组。总之,按从小到大已经排序。
    2,一个随机数字。比如5或者16等等,只是一个数字。
    计算条件是:
    随机数组按随机数字的方式组合,取元素最多的组合为结果。例如,以数组{1,1,1,3,4}来说,如果随机数字是5,想得到的结果是{1,1,3} (这个组合相加等于5) 
    -----------------------------如果随机数字是6,想得到的结果是{1,1,1,3} (这个组合相加等于6,{3,3}这个组合也是6,但是不满足组合中元素最多的这个条件)
    -----------------------------如果随机数字是11,想得到的结果是{1,1,1,3,3,4} (这个组合相加等于11)
    -----------------------------如果随机数字是16,想得到的结果是{1,3,4} (这个组合的倍数相加等于16)
      

  6.   

    -----------------------------如果随机数字是11,想得到的结果是{1,3,3,4} (这个组合相加等于11)
    又是写错。。谢谢 kong19 的热心回复 ,我看看
      

  7.   

    to : kong19哈哈,,真是抱歉,我在做一个排版优化计算的程序,算法上不知道还有没有其他方法
      

  8.   

                int[] rdmArray = { 1, 2, 3, 4, 5, 6 };
                int rendomInt = 20;
                int i = 0;
                List<int> list = new List<int>(rdmArray);
                List<int> resultList = new List<int>();
                bool res = false;
                if (rendomInt <= list.Sum())
                {
                    res = funcA(i, list, rendomInt, ref resultList);
                }
                if (!res && rendomInt % 2 == 0)
                {
                    res = funcA(i, list, rendomInt/2, ref resultList);
                }
                if (!res && rendomInt % 3 == 0)
                {
                    res = funcA(i, list, rendomInt/3, ref resultList);
                }funcA是递归函数
     bool funcA(int i, List<int> list, int result, ref List<int> resultList, int flg = 0)
            {
                bool res = false;
                if (i == result)
                {
                    res = true;
                    return res;
                }
                else if (i < result)
                {
                    if (flg == 0)
                    {
                        i = i + list[0];
                        resultList.Add(i);
                        list.RemoveAt(0);
                        funcA(i, list, result, ref resultList);
                    }
                    else
                    {
                        return false;
                    }
                }
                else
                {
                    i = i - resultList[0];
                    list.Add(resultList[0]);
                    resultList.RemoveAt(0);
                    funcA(i, list, result, ref resultList, 1);
                }
                return res;
            }
      

  9.   

    可能有边界判断什么的,都没有想,大概得思路就是 对list内的数据一个一个加算 ,然后判断结果,并返回所使用的数组元素。因为你的随机数组已经是从小到大排列,所以,从小加到大应该就是元素数最多的。递归函数有点问题修改了一下。。 bool funcA(int i, List<int> list, int result, ref List<int> resultList, int flg = 0)
            {
                bool res = false;
                if (i == result)
                {
                    res = true;
                }
                else if (i < result)
                {
                    if (flg == 0)
                    {
                        i = i + list[0];
                        resultList.Add(list[0]);
                        list.RemoveAt(0);
                        res = funcA(i, list, result, ref resultList);
                    }
                }
                else
                {
                    i = i - resultList[0];
                    list.Add(resultList[0]);
                    resultList.RemoveAt(0);
                    res = funcA(i, list, result, ref resultList, 1);
                }
                return res;
            }
      

  10.   


    我是.NET 2.0 ,参数内赋值不行。。
      

  11.   

    没循环阿,,, ref list的问题?那你把那个resultList定义成类下的私有成员变量,从参数中去掉。private List<int> resultList = new List<int>();
     
     bool funcA(int i, List<int> list, int result, int flg = 0)
    {...}
     
      

  12.   

     bool funcA(int i, List<int> list, int result, int flg = 0)
    {...}
     
    int flg = 0   这句出错信息如下:Default parameter specifiers are not permitted (CS0241) - D:\project\layout\MainForm.cs:351,82
      

  13.   

    bool funcA(int i, List<int> list, int result, int flg )
      if (flg == 0)
       {
    ......
       res = funcA(i, list, result, ref resultList, 0);
       }
       }
       else
       {
    ......
       res = funcA(i, list, result, ref resultList, 1);
       }
     
      

  14.   

    'System.Collections.Generic.List<int>' does not contain a definition for 'Sum' (CS0117) .NET 2.0 又没这东西,我看换个,呵呵