【问题标题】:How to transform this recursive function to iterative如何将此递归函数转换为迭代
【发布时间】:2021-07-06 18:18:09
【问题描述】:

我想用迭代版本替换这个函数。 我发现了一些接受唯一输入的版本。

private static void FindCombinations(char[] A, string output, ref int counter, int i, int n, int k)
{
    //Console.WriteLine($"i:{i} n:{n} k:{k} r:{output}");
    if (k == 0)
    {
        Console.WriteLine(output);
        counter++;
        return;
    }

    for (int j = i; j < n; j++)
    {
        FindCombinations(A, output + " " + A[j].ToString(), ref counter, j + 1, n, k - 1);
        while (j < n - 1 && A[j] == A[j + 1])
            j++;
    }
}
private static void Main(string[] args)
{
    string output = "";
    char[] keys = new char[] { 'A', 'A', 'B', 'C', 'D' };
    int count = 0;
    FindCombinations(keys, output, ref count, 0, keys.Length, 3);
    Console.WriteLine(count.ToString());
    Console.ReadKey();
}

【问题讨论】:

  • 你能告诉我们你到目前为止所做的尝试吗?此外,如果您能用语言解释该方法应该做什么,那也会有所帮助。
  • 这个算法来自techiedelight.com/find-distinct-combinations-of-given-length,我刚刚激活了输入有重复值的选项
  • 好极了,虽然最好用你自己的话写出来。请edit您的问题并将信息放在那里而不是链接(一个好的问题应该解释问题本身)。还请添加您所做的尝试。

标签: c# recursion iteration combinations


【解决方案1】:

下一次,你必须首先提供你的代码尝试,因为这个社区不是为你编程,而是试图帮助你编程。此外,我们将非常感谢您提供更多上下文。

无论如何,我认为下面的这种方法应该适合你。检查它是否适用于许多不同的情况,特别是边界条件。当然,这段代码可以优化(我只是试图模仿你的递归方法的作用),但由于你只是要求非递归代码,它应该就足够了:

 private static void FindCombinations2(char[] A, string output, ref int counter, int i, int n, int k)
        {
            i = Math.Max(i, 0);
            n = Math.Min(A.Length, n);
           
            char[] A_used = new char[n - i];
            Array.Copy(A, i, A_used, 0, n - i);

            int[] indexVect = new int[k];
            for (int j = 0; j < indexVect.Length; j++)
                indexVect[j] = j;

            int L = A_used.Length;

            string currentBase = output;
            int movingIndex = 0;
            while (true)
            {
                int indexA = indexVect[movingIndex];

                if (indexA >= L)
                {
                    indexVect[movingIndex] = movingIndex;
                    movingIndex--;                    
                    if (movingIndex < 0)
                        break;
                    currentBase = currentBase.Substring(0, currentBase.Length - 2);
                    continue;
                }

                char nextA = A_used[indexA];
                currentBase += " " + nextA;
                int t;
                for ( t= indexVect[movingIndex]+1; t< L; t++)
                {
                    if(A_used[t]!=nextA)
                    {
                        indexVect[movingIndex] = t;
                        break;
                    }
                }

                if (t >= L)
                    indexVect[movingIndex] = L;


                movingIndex++;
                if (movingIndex >= k)
                {
                    Console.WriteLine(currentBase);
                    counter++;
                    movingIndex--;
                    currentBase = currentBase.Substring(0, currentBase.Length - 2);
                }
                else
                    indexVect[movingIndex] = Math.Max(indexVect[movingIndex], indexA+1);
            }
            
        }

【讨论】:

  • 我的本意不是要伤害任何人!谢谢你的回答
猜你喜欢
  • 2014-03-01
  • 2020-05-08
  • 2021-11-03
  • 2020-08-20
  • 2021-10-05
  • 1970-01-01
  • 2016-04-24
相关资源
最近更新 更多