C# 如何打印加起来等于给定数字的组合?
C# How to print combinations that add upto given number?
我正在尝试使用动态编程实现 C# 的 "Count of different ways to express N as the sum of other numbers" 问题。
我的方法看起来像:
static int howManyWays(int n)
{
int [] safe= new int[n + 1];
// base cases
safe[0] = safe[1] = safe[2] = 1;
safe[3] = 2;
// iterate for all values from 4 to n
for (int i = 4; i <= n; i++)
safe[i] = safe[i - 1] + safe[i - 3]
+ safe[i - 4];
return safe[n];
}
比如我selectn
为n = 4
,那么我的结果是:
4
我想打印出这 4 个总和的组合:
1+1+1+1
1+3
3+1
4
有什么方法可以递归地或使用动态规划来做到这一点吗?
我的尝试是递归地获取一组组合:
static int[] Combs(int n)
{
int[] tusc = { };
if (n < 0)
yield break;
if (n == 0)
yield return tusc;
int[] X = { 1, 3, 4 };
for(int i = 0; i < X.Length; i++)
{
for(j = 0; j <= Combs(n-X[i]).Length; j++)
{
yield return X + j;
}
}
}
可在 python 中运行的原始代码,但不知道如何转换为 C#:
def combis(n):
if n < 0:
return
if n == 0:
yield []
for x in (1, 3, 4):
for combi in combis(n-x):
yield [x] + combi
>>> list(combis(5))
[[1, 1, 1, 1, 1], [1, 1, 3], [1, 3, 1], [1, 4], [3, 1, 1], [4, 1]]
这里有一个非常直接的翻译:
using System;
using System.Collections.Generic;
class MainClass
{
static IEnumerable<List<int>> Combs(int n)
{
if (n < 0)
{
yield break;
}
else if (n == 0)
{
yield return new List<int>();
}
foreach (int x in new List<int>() {1, 3, 4})
{
foreach (IEnumerable<int> combi in Combs(n - x))
{
var result = new List<int>() {x};
result.AddRange(combi);
yield return result;
}
}
}
public static void Main(string[] args)
{
foreach (IEnumerable<int> nums in Combs(5))
{
foreach (var i in nums)
{
Console.Write(i + ", ");
}
Console.WriteLine();
}
}
}
输出:
1, 1, 1, 1, 1,
1, 1, 3,
1, 3, 1,
1, 4,
3, 1, 1,
4, 1,
备注:
- 由于您使用的是
yield
,请将 Combs
header 更改为 return 和 IEnumerable<int>
而不是 int[]
。
- 使用列表而不是 fixed-length 数组和
List.AddRange
从 Python. 转换 +
列表连接操作
- 翻译
X
时有些混乱。在 Python 版本中,x
只是 {1, 3, 4}
选项列表中的一个元素,但在 C# 版本中,它是整个数组。
Combs(n-X[i]).Length
没有意义——它调用 Combs
,获取结果的长度,然后丢弃所有结果,所以它就像一个非常昂贵的计数器。 j
为您提供了一个计数器索引,而不是预期的 child Combs
调用中的元素之一。 foreach
是 Python 的 for .. in
循环最准确的翻译。
{1, 3, 4}
列表可能应该做成一个参数,以允许调用者控制其行为。
- 效率很低,因为重叠的子问题需要重新计算。改进它留作练习(无论如何这可能是你的下一步)。
我正在尝试使用动态编程实现 C# 的 "Count of different ways to express N as the sum of other numbers" 问题。 我的方法看起来像:
static int howManyWays(int n)
{
int [] safe= new int[n + 1];
// base cases
safe[0] = safe[1] = safe[2] = 1;
safe[3] = 2;
// iterate for all values from 4 to n
for (int i = 4; i <= n; i++)
safe[i] = safe[i - 1] + safe[i - 3]
+ safe[i - 4];
return safe[n];
}
比如我selectn
为n = 4
,那么我的结果是:
4
我想打印出这 4 个总和的组合:
1+1+1+1
1+3
3+1
4
有什么方法可以递归地或使用动态规划来做到这一点吗? 我的尝试是递归地获取一组组合:
static int[] Combs(int n)
{
int[] tusc = { };
if (n < 0)
yield break;
if (n == 0)
yield return tusc;
int[] X = { 1, 3, 4 };
for(int i = 0; i < X.Length; i++)
{
for(j = 0; j <= Combs(n-X[i]).Length; j++)
{
yield return X + j;
}
}
}
可在 python 中运行的原始代码,但不知道如何转换为 C#:
def combis(n):
if n < 0:
return
if n == 0:
yield []
for x in (1, 3, 4):
for combi in combis(n-x):
yield [x] + combi
>>> list(combis(5))
[[1, 1, 1, 1, 1], [1, 1, 3], [1, 3, 1], [1, 4], [3, 1, 1], [4, 1]]
这里有一个非常直接的翻译:
using System;
using System.Collections.Generic;
class MainClass
{
static IEnumerable<List<int>> Combs(int n)
{
if (n < 0)
{
yield break;
}
else if (n == 0)
{
yield return new List<int>();
}
foreach (int x in new List<int>() {1, 3, 4})
{
foreach (IEnumerable<int> combi in Combs(n - x))
{
var result = new List<int>() {x};
result.AddRange(combi);
yield return result;
}
}
}
public static void Main(string[] args)
{
foreach (IEnumerable<int> nums in Combs(5))
{
foreach (var i in nums)
{
Console.Write(i + ", ");
}
Console.WriteLine();
}
}
}
输出:
1, 1, 1, 1, 1,
1, 1, 3,
1, 3, 1,
1, 4,
3, 1, 1,
4, 1,
备注:
- 由于您使用的是
yield
,请将Combs
header 更改为 return 和IEnumerable<int>
而不是int[]
。 - 使用列表而不是 fixed-length 数组和
List.AddRange
从 Python. 转换 - 翻译
X
时有些混乱。在 Python 版本中,x
只是{1, 3, 4}
选项列表中的一个元素,但在 C# 版本中,它是整个数组。 Combs(n-X[i]).Length
没有意义——它调用Combs
,获取结果的长度,然后丢弃所有结果,所以它就像一个非常昂贵的计数器。j
为您提供了一个计数器索引,而不是预期的 childCombs
调用中的元素之一。foreach
是 Python 的for .. in
循环最准确的翻译。{1, 3, 4}
列表可能应该做成一个参数,以允许调用者控制其行为。- 效率很低,因为重叠的子问题需要重新计算。改进它留作练习(无论如何这可能是你的下一步)。
+
列表连接操作