繁体   English   中英

C#递归逻辑

[英]C# Recursion Logic

我正在尝试编写一个函数,其中嵌套循环的数量取决于传递给它的整数(numStroke)。 例如,当numStrokes为1时,执行的代码应为:

double checkProfitability(GameState state, int numStrokes)
{
    double[] possiblePayoffs = new double[50000];
    int pPIndex = 0;
    double sumOfPayoffs = 0;
    double averagePayoff = 0;

    for (int i = 0; i <= 5; i++)
    {
        // Populate possiblePayoffs[]
    }

    for (int ii = 0; ii < pPIndex; ii++)
    {
        sumOfPayoffs += possiblePayoffs[i];
    }

    averagePayoff = sumOfPayoffs / pPIndex;

    return averagePayoff;
}

当numStrokes为3时,应为:

double checkProfitability(GameState state, int numStrokes)
{
    double[] possiblePayoffs = new double[50000];
    int pPIndex = 0;
    double sumOfPayoffs = 0;
    double averagePayoff = 0;

    for (int i = 0; i <= 5; i++)
    {
        state.colors[i]++;

        for (int j = 0; j <= 5; j++)
        {
            state.colors[j]++;

            for (int k = 0; k <= 5; k++)
            {
                // Populate possiblePayoffs[]
            }

            state.colors[j]--;
        }

        state.colors[i]--;
    }

    for (int ii = 0; ii < pPIndex; ii++)
    {
        sumOfPayoffs += possiblePayoffs[i];
    }

    averagePayoff = sumOfPayoffs / pPIndex;

    return averagePayoff;
}

链接是numStrokes为6时的另一个示例,以防万一我想做的事情还不清楚:

http://hastebin.com/hemolikodo.avrasm

到目前为止,我已经提出了以下尝试来实现numStrokes数量的嵌套循环,但是它不起作用(如果没有其他原因,因为该函数在递归调用自身时尝试创建另一个int i副本)。 我不确定这段代码是否正确。 我什至不确定我是否应该尝试递归执行此操作。 我考虑只使用基于numStrokes的值执行代码的巨型if语句,但是更通用的实现似乎更可取。

double checkProfitability(GameState state, int numStrokes, int i)
{
    double[] possiblePayoffs = new double[50000];
    int pPIndex = 0;
    double sumOfPayoffs = 0;
    double averagePayoff = 0;

    if (numStrokes == 0)
    {
        // Populate possiblePayoffs[]
    }
    else
    {
        for (int i = 0; i <= 5 && numStrokes >= 1; i++)
        {
            checkProfitability(state, --numStrokes, i);
        }
    }

    for (int ii = 0; ii < pPIndex; ii++)
    {
        sumOfPayoffs += possiblePayoffs[ii];
    }

    averagePayoff = sumOfPayoffs / pPIndex;
    richTextBox1.Text = averagePayoff.ToString();

    return averagePayoff;
}

谁能解释如何正确执行此操作?

编辑:问题是直到运行时,我才知道需要多少个嵌套循环。

for (int i = 0; i < Math.Pow(6, numStrokes); i++)
{
    int innerForIndex = i;
    for (int j = 0; j < numStrokes; j++)
    {
        colors[innerForIndex % 6]++;
        innerForIndex /= 6;
    }

    //populate your possiblePayoffs[]

    innerForIndex = i;
    for (int j = 0; j < numStrokes; j++)
    {
        colors[innerForIndex % 6]--;
        innerForIndex /= 6;
    }

}

numStrokes for循环从0到5(含0和5)的循环意味着您总共有Math.Pow(6, numStrokes)元素。 您可以使用内部循环索引来增加/减少一些cololrs数组。 这些索引可以很容易地从元素编号中计算出来。 对于numStroke == 3示例,可以将k计算为innerForIndex % 6 ,j计算为(innerForIndex / 6) % 6 ,i计算为((innerForIndex / 6) / 6) % 6

我认为这是最接近的解决方案。

首先,这是checkProfitability方法:

double checkProfitability(GameState state, int numStrokes)
{
    var possiblePayoffs = new double[50000];
    computePayoffs(state, possiblePayoffs, Enumerable.Empty<int>(), numStrokes);
    var averagePayoff = possiblePayoffs.Select(x => (double)x).Average();
    richTextBox1.Text = averagePayoff.ToString();
    return averagePayoff;
}

现在,递归位于computePayoffs方法中:

void computePayoffs(GameState state, int[] possiblePayoffs,
    IEnumerable<int> values, int numStrokes)
{
    if (numStrokes == 0)
    {
        // Populate possiblePayoffs[]
    }
    else
    {
        for (int i = 0; i <= 5; i++)
        {
            state.colors[i]++;
            computePayoffs(
                    state,
                    possiblePayoffs,
                    values.Concat(new [] { i }),
                    numStrokes - 1);
            state.colors[i]--;
        }
    }
}
for (int i = 0; i <= 5 * numstrokes; i++)
{
    // Populate possiblePayoffs[]
    if(i % 5 == 0)
    {
        //start of next loop
    }
}

为什么不这样做呢?

问题尚不清楚。 但是我认为递归将帮助您解决这类情况。 我能理解的是,您需要做一些循环numstocks * 6(下面的代码将循环这么多时间。如果是这种情况,代码的结构将为(未测试。可能需要进行一些小的修改)

double checkProfitability(GameState state, int numStrokes)
{
if(numStrokes!=0)
{
    for (int i = 0; i <= 5; i++)
    {
       checkProfitability(state,numStrokes-1);
    }
}
//your code  //calls this code numStrokes*6 times
}

还要多提防stackoverflow异常

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM