随机数组 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 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 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 ...重新排列了一下数字,望多指教- -
------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,不知我这样解释能否看得明白。
------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,不知我这样解释能否看得明白。
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);
}
}
前提条件是:
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)
又是写错。。谢谢 kong19 的热心回复 ,我看看
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;
}
{
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;
}
我是.NET 2.0 ,参数内赋值不行。。
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
if (flg == 0)
{
......
res = funcA(i, list, result, ref resultList, 0);
}
}
else
{
......
res = funcA(i, list, result, ref resultList, 1);
}