这里输入 y 为负数,等 y 减到 −2147483648 时,就会爆 int,变成 2147483647,所以不会无限递归,只是会爆栈。
所以求问是不是应该打叉。
原题:
27.当输入为“2 3”时,customFunction(2,3)的返回值为“64”。( )
代码:
#include <iostream>
#include <cmath>
using namespace std;
int customFunction(int a, int b) {
cout<<b<<endl;
if (b == 0) {
return a;
}
return a + customFunction(a , b - 1);
}
int main() {
int x, y;
cin >> x >> y;
int result = customFunction(x, y);
cout << pow(result, 2) << endl;
return 0;
}