英文:
I need a c# function to determine the combinations of n max integers. Thanks
问题 {#heading}
与提供 n 选 k 值组合的其他示例不同,我有一个包含 n 的数组,我需要得到从 0 到 n 的所有值的组合。例如:
如果我有一个数组 5,8,3,4,9,函数将返回:
0,0,0,0,0
0,0,0,0,1
...
1,1,1,1,1
1,1,1,1,2
...
5,8,3,4,9
我可以使用一个过程来找到最大值为 n 的 k 值的组合,但它提供了太多的组合(其中许多组合是无效的,我必须丢弃):
```csharp
private static IEnumerable<IEnumerable<T>> GetCombinations<T>(IEnumerable<T> list, int length) where T : IComparable
{
if (length == 1) return list.Select(t => new T[] { t });
return GetCombinations(list, length - 1)
.SelectMany(t => list.Where(o => o.CompareTo(t.Last()) >= 0),
(t1, t2) => t1.Concat(new T[] { t2 }));
}
0,0,0,0,0
0,0,0,0,1
...
9,9,9,9,9
<details>
<summary>英文:</summary>
Unlike other examples that provide combinations of n for k values, I have an array of n and I need a combination of all values from 0 - n. For example:
If I have an array of 5,8,3,4,9 the function would return:
0,0,0,0,0
0,0,0,0,1
...
1,1,1,1,1
1,1,1,1,2
...
5,8,3,4,9
I can accomplish this using a process to find the combinations of a max of n for k values, but it provides too many combinations (many of these combinations are invalid that I have to throw out):
private static IEnumerable<IEnumerable<T>> GetCombinations<T>(IEnumerable<T> list, int length) where T : IComparable
{
if (length == 1) return list.Select(t => new T[] { t });
return GetCombinations(list, length - 1)
.SelectMany(t => list.Where(o => o.CompareTo(t.Last()) >= 0),
(t1, t2) => t1.Concat(new T[] { t2 }));
}
0,0,0,0,0
0,0,0,0,1
...
9,9,9,9,9
\</details\>
答案1
===
得分: 0
以下是您要翻译的代码部分:
```csharp
这是一个适用于数值类型的实现:
public static IEnumerable<T[]> Combinations<T>(T[] values) where T : INumber<T>
{
var current = new T[values.Length];
do
{
yield return current.ToArray();
}
while (increment());
bool increment()
{
for (int index = current.Length - 1; index >= 0; --index)
{
if (++current[index] <= values[index])
return true;
current[index] = default!;
}
return false;
}
}
测试代码:
public static void Main()
{
var array = new[] { 5, 8, 3, 4, 9 };
foreach (var combination in Combinations(array))
{
Console.WriteLine(string.Join(", ", combination));
}
}
然而,您的测试代码正在使用 `IComparable` - 我无法看出它如何适用于非数值的 `IComparable` 类型,例如 `string` 和 `DateTime`。
例如,对于输入数组 `{ "Tom", "Dick", "Harry" }`,会有什么含义?
</code></pre>
<details>
<summary>英文:</summary>
<p>Here's an implementation that would work for numeric types:</p>
<pre><code>public static IEnumerable&lt;T[]&gt; Combinations&lt;T&gt;(T[] values) where T: INumber&lt;T&gt;
{
var current = new T[values.Length];
do
{
yield return current.ToArray();
}
while (increment());
bool increment()
{
for (int index = current.Length - 1; index &gt;= 0; --index)
{
if (++current[index] &lt;= values[index])
return true;
current[index] = default!;
}
return false;
}
}
</code></pre>
<p>Test code:</p>
<pre><code>public static void Main()
{
var array = new [] { 5, 8, 3, 4, 9 };
foreach (var combination in Combinations(array))
{
Console.WriteLine(string.Join(&quot;, &quot;, combination));
}
}
</code></pre>
<p>However, your test code is using <code>IComparable</code> - I can't see how that would work with non-numeric <code>IComparable</code> types such as <code>string</code> and <code>DateTime</code>.</p>
<p>For example, what would it mean for an input array of <code>{&quot;Tom&quot;, &quot;Dick&quot;, &quot;Harry&quot;}</code> ?</p>
</details>
<p></p>
</div>
```