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

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

I need a c# function to determine the combinations of n max integers. Thanks

问题

  1. 与提供 n k 值组合的其他示例不同,我有一个包含 n 的数组,我需要得到从 0 n 的所有值的组合。例如:
  2. 如果我有一个数组 5,8,3,4,9,函数将返回:
  3. 0,0,0,0,0
  4. 0,0,0,0,1
  5. ...
  6. 1,1,1,1,1
  7. 1,1,1,1,2
  8. ...
  9. 5,8,3,4,9
  10. 我可以使用一个过程来找到最大值为 n k 值的组合,但它提供了太多的组合(其中许多组合是无效的,我必须丢弃):
  11. ```csharp
  12. private static IEnumerable<IEnumerable<T>> GetCombinations<T>(IEnumerable<T> list, int length) where T : IComparable
  13. {
  14. if (length == 1) return list.Select(t => new T[] { t });
  15. return GetCombinations(list, length - 1)
  16. .SelectMany(t => list.Where(o => o.CompareTo(t.Last()) >= 0),
  17. (t1, t2) => t1.Concat(new T[] { t2 }));
  18. }

0,0,0,0,0
0,0,0,0,1
...
9,9,9,9,9

  1. <details>
  2. <summary>英文:</summary>
  3. 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:
  4. If I have an array of 5,8,3,4,9 the function would return:
  5. 0,0,0,0,0
  6. 0,0,0,0,1
  7. ...
  8. 1,1,1,1,1
  9. 1,1,1,1,2
  10. ...
  11. 5,8,3,4,9
  12. 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 }));
}

  1. 0,0,0,0,0
  2. 0,0,0,0,1
  3. ...
  4. 9,9,9,9,9
  5. </details>
  6. # 答案1
  7. **得分**: 0
  8. 以下是您要翻译的代码部分:
  9. ```csharp
  10. 这是一个适用于数值类型的实现:
  11. public static IEnumerable<T[]> Combinations<T>(T[] values) where T : INumber<T>
  12. {
  13. var current = new T[values.Length];
  14. do
  15. {
  16. yield return current.ToArray();
  17. }
  18. while (increment());
  19. bool increment()
  20. {
  21. for (int index = current.Length - 1; index >= 0; --index)
  22. {
  23. if (++current[index] <= values[index])
  24. return true;
  25. current[index] = default!;
  26. }
  27. return false;
  28. }
  29. }
  30. 测试代码:
  31. public static void Main()
  32. {
  33. var array = new[] { 5, 8, 3, 4, 9 };
  34. foreach (var combination in Combinations(array))
  35. {
  36. Console.WriteLine(string.Join(", ", combination));
  37. }
  38. }
  39. 然而,您的测试代码正在使用 `IComparable` - 我无法看出它如何适用于非数值的 `IComparable` 类型,例如 `string` 和 `DateTime`。
  40. 例如,对于输入数组 `{ "Tom", "Dick", "Harry" }`,会有什么含义?
英文:

Here's an implementation that would work for numeric types:

  1. public static IEnumerable&lt;T[]&gt; Combinations&lt;T&gt;(T[] values) where T: INumber&lt;T&gt;
  2. {
  3. var current = new T[values.Length];
  4. do
  5. {
  6. yield return current.ToArray();
  7. }
  8. while (increment());
  9. bool increment()
  10. {
  11. for (int index = current.Length - 1; index &gt;= 0; --index)
  12. {
  13. if (++current[index] &lt;= values[index])
  14. return true;
  15. current[index] = default!;
  16. }
  17. return false;
  18. }
  19. }

Test code:

  1. public static void Main()
  2. {
  3. var array = new [] { 5, 8, 3, 4, 9 };
  4. foreach (var combination in Combinations(array))
  5. {
  6. Console.WriteLine(string.Join(&quot;, &quot;, combination));
  7. }
  8. }

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:

确定