如何计算递归功能的调用(增量1)|C

How to count the calls of a recursive function (Increment by 1) | C++

本文关键字:增量 调用 递归 何计算 计算 功能      更新时间:2023-10-16

我具有以下代码。它接受输入,并按行步骤打印以解决河内问题。我要做的就是在每行step1,step2等上打印,从1到步骤的末尾如下示例:


输入磁盘数:3
步骤1:1-> 2将一个磁盘从原始钉移到额外的钉
步骤2:1-> 3将一个磁盘从原始钉移到目的地钉
步骤3:2-> 3将一个磁盘从多余的钉移到目的地钉

  #include <iostream>
int count=1;
     void ToH(int dskToMv, int cLocation, string orpeg, int tmpLocation, string expeg, int fLocation, string depeg)
    {
         if( dskToMv != 0 ) 
        {
count++;
            ToH( dskToMv-1, cLocation, orpeg, fLocation, depeg, tmpLocation, expeg);
            cout<<"Step "<<count<<": Move one disk from the "<< orpeg << " to the " << depeg
                << " " << cLocation << " -> " << fLocation << endl;
            ToH( dskToMv-1, tmpLocation, expeg, cLocation, orpeg, fLocation, depeg);
        }
    }
    int main()
    {
        int c;
        cout << "Enter the number of disks: ";
        cin >> c;
        ToH(c, 1, "original peg ", 2, "extra peg", 3, "destination peg");
    }

在递归中,当您需要"保留"每个迭代的内容时,有几种方法:

  1. 原始的全局变量 - 这些仅允许保持单个状态。如果递归分支等等,则可能不起作用,因为一个分支会覆盖另一个分支正在使用的值。仅仅存储诸如计数之类的东西,这些趋势往往可以很好地工作。

  2. 将参数传递到函数 - 参数到一个函数的参数存储在堆栈上。当调用函数时,将它们推开,因此对于每次递归,我们最终都会得到每个迭代调用参数值的"堆栈"。这与递归非常有效,因为不同的分支不会破坏其他值(弹出发生在函数呼叫的末尾,只弹出调用的值)。

  3. 使用复杂的全局数据结构。这些本质上是在函数调用之外存储参数,并允许发生更多复杂的事情(例如,使用队列代替堆栈),但需要更多的工作。因为它们是功能外部的,如果需要,可以通过其他分支来查看它们。

您需要做的就是声明全局int,然后在函数调用中将其递增。每次称为函数时,值都会增加,因此计算函数调用的数量。由于它只是增加了分支没有问题(因为每个分支都会做同样的事情,因此无法区分)。

int count = 0;
 void ToH(int dskToMv, int cLocation, string orpeg, int tmpLocation, string expeg, int fLocation, string depeg)
{
   count++;
     if( dskToMv != 0 ) 
    {
        ToH( dskToMv-1, cLocation, orpeg, fLocation, depeg, tmpLocation, expeg);
        cout<<" Move one disk from the "<< orpeg << " to the " << depeg
            << " " << cLocation << " -> " << fLocation << " - " << count << endl;
        ToH( dskToMv-1, tmpLocation, expeg, cLocation, orpeg, fLocation, depeg);
    }
}

另外,如果要允许回溯,因此实际上计数"级别",则可以使用一个参数:

 void ToH(int dskToMv, int cLocation, string orpeg, int tmpLocation, string expeg, int fLocation, string depeg, int count = 0)
{
     if( dskToMv != 0 ) 
    {
        ToH( dskToMv-1, cLocation, orpeg, fLocation, depeg, tmpLocation, expeg, count + 1);
        cout<<" Move one disk from the "<< orpeg << " to the " << depeg
            << " " << cLocation << " -> " << fLocation << " - " << count << endl;
        ToH( dskToMv-1, tmpLocation, expeg, cLocation, orpeg, fLocation, depeg, count + 1);
    }
}

并非所有方法都是相同的。当您学会将递归视为在大型分支数据结构上的一种"搜索"时,考虑这些事情会更容易。当您仅使用等同于迭代的简单递归时,这些不同的方法之间没有太大的区别。

您可以尝试使用静态变量。每次调用该函数时,都会增加此变量。

#include <iostream>
 void ToH(int dskToMv, int cLocation, string orpeg, int tmpLocation, string expeg, int fLocation, string depeg)
{
    static int count=0 
    if( dskToMv != 0 ) 
    {
        count++;
        ToH( dskToMv-1, cLocation, orpeg, fLocation, depeg, tmpLocation, expeg);
        cout<<"Step "<<count<<": Move one disk from the "<< orpeg << " to the " << depeg
            << " " << cLocation << " -> " << fLocation << endl;
        ToH( dskToMv-1, tmpLocation, expeg, cLocation, orpeg, fLocation, depeg);
    }
}
int main()
{
    int c;
    cout << "Enter the number of disks: ";
    cin >> c;
    ToH(c, 1, "original peg ", 2, "extra peg", 3, "destination peg");
}

希望这有帮助