如果我的输入数字是2 ...我明白该功能将采用先前的答案并乘以2直到该数字将大于25
2 * 2 = 4
4 * 2 = 8
8 * 2 = 16
16 * 2 = 32 ...程序停止,因为num为32
然后我的最终号码是62我如何获得该号码>
#include <iostream>
using namespace std;
int myRecursiveFunction (int num)
{
if (num >= 25) // stopping condition
return num;
else
cout << "number is at " << num << endl;
return num = num + myRecursiveFunction (num * 2);
}
int main()
{
int num, num1;
cout << "Enter a number: ";
cin >> num;
num1 = myRecursiveFunction(num);
cout << "Final number is: " << num1;
return 0;
}
答案 0 :(得分:2)
你想这样做:
2 * 2 = 4
4 * 2 = 8
8 * 2 = 16
16 * 2 = 32 ...程序停止,因为num为32
你正在做 return num = num + myRecursiveFunction(num * 2); 返回32 + 16 + 8 + 4 + 2 = 62答案 1 :(得分:0)
2 + 4 + 8 + 16 + 32 = 62 每次都会添加返回值。