我不懂递归函数。
我写了这段代码来帮助我,但我不明白为什么它会这样工作。
它向后打印从 0 到数字 n/2 i 输入的步骤,但不知道是什么使它打印了它从低到高跳过的每一步,因为它是递归的。我很近,但还没有……
#include <iostream>
#include <conio.h>
using namespace std;
int recursiv(int );
int times;
int main(){
int x;
cout<<"Imput number\n";
cin>>x;
recursiv(x);
getch();
return 0;
}
int recursiv(int x){
times++;
if(x)
recursiv(x/2);
cout<<"We are now at "<<x/2<<endl;
if (!x)
cout<< "We reached "<<x<<" but it took "<<times-1<< " steps\n";
return 0;
}