帮助创建一个递归函数C# [英] Help with Creating a Recursive Function C#

查看:134
本文介绍了帮助创建一个递归函数C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建将运行各种模式模拟一个生产厂能够运行预测中的应用。该工厂每天可以一个模式中运行,所以我写这将增加了每一天最能最大限度地发挥植物的产量和最好的对齐与提供的销售预测号码选择的不同模式的功能。此数据将被装入,然后将被用于计算该植物的预测输出模式对象的数组。

I am creating a forecasting application that will run simulations for various "modes" that a production plant is able to run. The plant can run in one mode per day, so I am writing a function that will add up the different modes chosen each day that best maximize the plant’s output and best aligns with the sales forecast numbers provided. This data will be loaded into an array of mode objects that will then be used to calculate the forecast output of the plant.

我创建的功能要做到这一点,但是,我需要的模式和工作日内,以使他们递归,使我能够处理任何数量的(合理范围内) (其中根据生产需要而异)。下面列出的是使用循环来模拟我想要做我的代码。有人能指出我在正确的方向,以创建一个递归函数来替代多个for循环的需要?

I have created the functions to do this, however, I need to make them recursive so that I am able to handle any number (within reason) of modes and work days (which varies based on production needs). Listed below is my code using for loops to simulate what I want to do. Can someone point me in the right direction in order to create a recursive function to replace the need for multiple for loops?

如果当时有四种模式的方法GetNumbers4会,并GetNumbers5将5种模式。 INT开始将工作的天数。

Where the method GetNumbers4 would be when there were four modes, and GetNumbers5 would be 5 modes. Int start would be the number of work days.

  private static void GetNumber4(int start)
    {
        int count = 0;
        int count1 = 0;          

        for (int i = 0; 0 <= start; i++)
        {
            for (int j = 0; j <= i; j++)
            {

                for (int k = 0; k <= j; k++)
                {
                    count++;

                     for (int l = 0; l <= i; l++)
                     {
                         count1 = l;
                     }

                     Console.WriteLine(start + " " + (count1 - j) + " " + (j - k) + " " + k);
                     count1 = 0;
                }  

            }
            start--;

        }
        Console.WriteLine(count);

    }

    private static void GetNumber5(int start)
    {
        int count = 0;
        int count1 = 0;

        for (int i = 0; 0 <= start; i++)
        {
            for (int j = 0; j <= i; j++)
            {

                for (int k = 0; k <= j; k++)
                {

                    for (int l = 0; l <= k; l++)
                    {
                        count++;
                        for (int m = 0; m <= i; m++)
                        {
                            count1 = m;
                        }
                        Console.WriteLine(start + " " + (count1 - j) + " " + (j - k) + " " + (k - l) + " " + l);
                        count1 = 0;
                    }

                }

            }
            start--;

        }
        Console.WriteLine(count);

    }



编辑:

我认为,这将是更有帮助,如果我放弃什么,我试图做一个例子。例如,如果一个工厂可以在三种模式下的A运行,B,C和有三个工作日内,则代码将返回以下结果。

I think that it would be more helpful if I gave an example of what I was trying to do. For example, if a plant could run in three modes "A", "B", "C" and there were three work days, then the code will return the following results.

3  0  0
2  1  0
2  0  0
1  2  0
1  1  1
1  0  2
0  3  0
0  2  1
0  1  2
0  0  3

的数字系列代表了三种模式AB C.我将加载这些结果到具有相应的生产速率的模式对象。这样做可以使我快捷创建每个可能的组合列表;它,而不是让我发生的频率。

The series of numbers represent the three modes A B C. I will load these results into a Modes object that has the corresponding production rates. Doing it this way allows me to shortcut creating a list of every possible combination; it instead gives me a frequency of occurrence.

建立在已经提供的解决方案之一,我愿做这样的事情。

Building on one of the solutions already offered, I would like to do something like this.

    //Where Modes is a custom classs
    private static Modes GetNumberRecur(int start, int numberOfModes)
    {
        if (start < 0)
        {
            return Modes;

        }

        //Do work here
        GetNumberRecur(start - 1);
    }



感谢谁已经提供的输入每个人。

Thanks to everyone who have already provided input.

推荐答案

调用GetNumber(5,X)应产生相同的结果GetNumber5(X):

Calling GetNumber(5, x) should yield the same result as GetNumber5(x):

static void GetNumber(int num, int max) {
    Console.WriteLine(GetNumber(num, max, ""));
}
static int GetNumber(int num, int max, string prefix) {
    if (num < 2) {
        Console.WriteLine(prefix + max);
        return 1;
    }
    else {
        int count = 0;
        for (int i = max; i >= 0; i--)
            count += GetNumber(num - 1, max - i, prefix + i + " ");
        return count;
    }
}

这篇关于帮助创建一个递归函数C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆