组合,带重复次数C# [英] Combinations With Repetitions C#
问题描述
我需要与重复组合的援助。已经找遍了网上,虽然我发现了几个例子,我不能完全理解他们。我的目标是简单的函数(CombinationsWithRepetiion)接收列表的项目(在此情况下,整数值)和长度(表示每个组合可以有多长),并返回包含结果的列表。
列表< INT>输入=新列表与所述; INT>(){1,2,3}
CombinationsWithRepetition(输入,长度);
结果:
长度= 1: 1,2,3
长度= 2:11,12,13,21,22,23,31,32,33
长度= 3:111,112 ....
我希望有人可以帮助我,感谢你在
< DIV CLASS =h2_lin>解决方案
递归
好吧,
在这里是C#版本 - 我走你通过它
静态的IEnumerable<串GT; CombinationsWithRepition(IEnumerable的< INT>输入,INT长度)
{
如果(长度LT; = 0)
收益率的回报,;
,否则
{
的foreach(VAR我输入)
的foreach(在CombinationsWithRepition变种C(输入,长度1))
收益率的回报i.ToString() +℃;
}
}
首先检查边界情况下的递归(在这种情况下,如果长度LT = 0
) - 在这种情况下,答案是空字符串(顺便说一句:我选择返回字符串作为你没有说你真正需要的是什么 - 应该是很容易改变)
在你看每一个输入 I
和recursivley采取任何其他情况下下一个更小的组合,只是插EM在一起(使用字符串concatination因为我想字符串)。
我希望你明白了的IEnumerable
/ 收益
的东西 - 如果不是在评论中这样说,请
下面是一个示例输出
的foreach(在CombinationsWithRepition变种C(新INT [] {1,2,3},3))
Console.WriteLine(C);
111
112
113
...
332
333
数字转换
下面使用我在下面的评论勾勒的想法,并与堆栈溢出异常没有问题(递归可能会为大lenght) - 这太假设字符串,因为它们更容易使用(我可以做一个简单的 PadLeft
来简化事情)
静态字符串转换(串符号,诠释数量,诠释totalLen)
{
VAR的结果=;
VAR LEN = symbols.Length;
变种nullSym =符号[0];
,而(数大于0)
{
VAR指数=编号%LEN;
号=号/ LEN;
结果=符号[指数] +结果;
}
返回result.PadLeft(totalLen,nullSym);
}
静态的IEnumerable<串GT; CombinationsWithRepition(串符号,诠释LEN)
{
为(VAR I = 0; I< Math.Pow(symbols.Length,LEN);我++)
收益率的回报转换(符号,我,LEN);
}
I need assistance with Combinations with Repetition. Have searched all over the net and although I found a few examples I can't understand them completely. My goal is simple a function (CombinationsWithRepetiion) receives list with items (in this case integer values) and length (that represents how long each combination can be) and returns a list containing the result.
List<int> input = new List<int>() {1, 2, 3}
CombinationsWithRepetition(input, length);
result:
length = 1: 1, 2, 3
length = 2: 11,12,13,21,22,23,31,32,33
length = 3: 111,112 ....
I hope someone helps me and thank you in advance!
recursion
Ok,
here is the C# version - I walk you through it
static IEnumerable<String> CombinationsWithRepition(IEnumerable<int> input, int length)
{
if (length <= 0)
yield return "";
else
{
foreach(var i in input)
foreach(var c in CombinationsWithRepition(input, length-1))
yield return i.ToString() + c;
}
}
First you check the border-cases for the recursion (in this case if length <= 0
) - in this case the answer is the empty string (btw: I choose to return strings as you did not say what your really needed - should be easy to change).
In any other case you look at each input i
and recursivley take the next-smaller combinations and just plug em together (with String-concatination because I wanted strings).
I hope you understand the IEnumerable
/yield
stuff - if not say so in the comments please.
Here is a sample output:
foreach (var c in CombinationsWithRepition(new int[]{1,2,3}, 3))
Console.WriteLine (c);
111
112
113
...
332
333
converting numbers
The following uses the idea I sketched in the comment below and has no problems with stack-overflow exceptions (recursion might for big lenght) - this too assumes strings as they are easier to work with (and I can do a simple PadLeft
to simplify things)
static String Convert(string symbols, int number, int totalLen)
{
var result = "";
var len = symbols.Length;
var nullSym = symbols [0];
while (number > 0)
{
var index = number % len;
number = number / len;
result = symbols [index] + result;
}
return result.PadLeft (totalLen, nullSym);
}
static IEnumerable<String> CombinationsWithRepition(string symbols, int len)
{
for (var i = 0; i < Math.Pow(symbols.Length,len); i++)
yield return Convert (symbols, i, len);
}
这篇关于组合,带重复次数C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!