如何将此 Python 脚本翻译成 C#?

我有一个用 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))

目前我不明白如何在 C# 中实现这段代码,让我分心的是那些产量以及我的方法应该以哪种类型编写。我目前拥有的东西不起作用且无法理解


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;

                }


            }


        }

当 n 等于 5 时,python 脚本的输出为:


>>> list(combis(5))

[[1, 1, 1, 1, 1], [1, 1, 3], [1, 3, 1], [1, 4], [3, 1, 1], [4, 1]]

当 n 等于 4 时:


[[1, 1, 1, 1], [1, 3], [3, 1], [4]]          


慕少森
浏览 136回答 1
1回答

三国纷争

using System;using System.Collections.Generic;namespace HowToTranslateInCSharp{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; static IEnumerable<List<int>> Combis(int n)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (n >= 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (n == 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return new List<int>();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var x in new[] {1, 3, 4})&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var combi in Combis(n - x))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var list = new List<int>() {x};&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.AddRange(combi);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; yield return list;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var result = Combis(5);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (var list in result)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine($"[{string.Join(" ", list)}]");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}输出是[1 1 1 1 1][1 1 3][1 3 1][1 4][3 1 1][4 1]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python