1 回答
TA贡献1804条经验 获得超7个赞
using System;
using System.Collections.Generic;
namespace HowToTranslateInCSharp
{
class Program
{
static IEnumerable<List<int>> Combis(int n)
{
if (n >= 0)
{
if (n == 0)
yield return new List<int>();
else
{
foreach (var x in new[] {1, 3, 4})
{
foreach (var combi in Combis(n - x))
{
var list = new List<int>() {x};
list.AddRange(combi);
yield return list;
}
}
}
}
}
static void Main(string[] args)
{
var result = Combis(5);
foreach (var list in result)
{
Console.WriteLine($"[{string.Join(" ", list)}]");
}
}
}
}
输出是
[1 1 1 1 1]
[1 1 3]
[1 3 1]
[1 4]
[3 1 1]
[4 1]
添加回答
举报