在一次测试考试中,我们被告知要找出一些表达式的值。
除了 1 之外的所有内容都是明确的,即"20"[1]. 我以为它是数字的第一个索引,所以0,但是用它打印的计算机进行测试48。
这个“功能”到底是做什么的?
我正在学习尾递归,在解决这个问题之前,我想对代码片段是否尾递归进行是/否的回答。
int fib_in(int n, int current, int prev) {
if (n == 1 || n == 2) { // n = 1 or 2 both gives fib number 1
return current;
}
return fib_in(n - 1, current + prev, current); // recursive call, current gets updated and new prev is the current, so were going backwards if that makes sense
}
int fib(int n) {
return fib_in(n, 1, 1); // 1 and 1 is the 2 first fib numbers so …Run Code Online (Sandbox Code Playgroud)