英文:
I need a c# function to determine the combinations of n max integers. Thanks
问题
与提供 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" }`,会有什么含义?
英文:
Here's an implementation that would work for numeric types:
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;
}
}
Test code:
public static void Main()
{
var array = new [] { 5, 8, 3, 4, 9 };
foreach (var combination in Combinations(array))
{
Console.WriteLine(string.Join(", ", combination));
}
}
However, your test code is using IComparable
- I can't see how that would work with non-numeric IComparable
types such as string
and DateTime
.
For example, what would it mean for an input array of {"Tom", "Dick", "Harry"}
?
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论