如何计算递归功能的调用(增量1)|C
How to count the calls of a recursive function (Increment by 1) | C++
我具有以下代码。它接受输入,并按行步骤打印以解决河内问题。我要做的就是在每行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");
}
在递归中,当您需要"保留"每个迭代的内容时,有几种方法:
-
原始的全局变量 - 这些仅允许保持单个状态。如果递归分支等等,则可能不起作用,因为一个分支会覆盖另一个分支正在使用的值。仅仅存储诸如计数之类的东西,这些趋势往往可以很好地工作。
-
将参数传递到函数 - 参数到一个函数的参数存储在堆栈上。当调用函数时,将它们推开,因此对于每次递归,我们最终都会得到每个迭代调用参数值的"堆栈"。这与递归非常有效,因为不同的分支不会破坏其他值(弹出发生在函数呼叫的末尾,只弹出调用的值)。
-
使用复杂的全局数据结构。这些本质上是在函数调用之外存储参数,并允许发生更多复杂的事情(例如,使用队列代替堆栈),但需要更多的工作。因为它们是功能外部的,如果需要,可以通过其他分支来查看它们。
您需要做的就是声明全局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");
}
希望这有帮助
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 增量运算符与后缀混淆
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在c++类上调用void函数
- 为什么 std::unique 不调用 std::sort?
- 调用专用模板时出错"no matching function for call to [...]"
- 选择要调用的构造函数
- C++为什么尽管我调用了void函数,它却不起作用
- 构造函数正在调用一个使用当前类类型的函数
- 为什么在C 中超载邮政增量运算符两次调用构造函数
- 如何计算递归功能的调用(增量1)|C
- C ++,我的程序在调用list.erase后"go",没有增量迭代器?
- 如何使我的代码知道我是在调用增量前运算符还是增量后运算符
- 在sizeof(++n)表达式中未调用增量运算符
- 为什么有两个额外的析构函数调用,因为我只创建了两个对象并使用重载赋值运算符和增量运算符