我需要一个用C#编写的函数来确定n个最大整数的组合。谢谢。

huangapple go评论106阅读模式
英文:

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&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;
    }
}

Test 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));
    }
}

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 {&quot;Tom&quot;, &quot;Dick&quot;, &quot;Harry&quot;} ?

huangapple
  • 本文由 发表于 2023年8月10日 22:20:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76876620.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定