我有以下数学表达式:
; f(n) = f(n - 1) + f(n - 2) where n >= 2
; f(n) = n where n < 2`
Run Code Online (Sandbox Code Playgroud)
我将其转换为正常的递归LISP调用:
(define (f n)
(cond ((< n 2) n)
(else (+ (f (- n 1))
(f (- n 2))))))
Run Code Online (Sandbox Code Playgroud)
我如何将上述内容转换为尾递归过程?我不习惯函数式编程,所以我有点挣扎.
import java.math.BigInteger;
import java.util.HashMap;
/**
*
* @author cypronmaya
*/
public class test {
static HashMap<Integer, BigInteger> cache = new HashMap<Integer, BigInteger>();
public static void main(String[] args) {
System.out.println(factorial(20000));
}
public static BigInteger factorial(int n) {
BigInteger ret;
if (n == 0) {
return BigInteger.ONE;
}
if (null != (ret = cache.get(n))) {
return ret;
}
ret = BigInteger.valueOf(n).multiply(factorial(n - 1));
cache.put(n, ret);
return ret;
}
}
Run Code Online (Sandbox Code Playgroud)
java.util.HashMap.get中的线程"main"java.lang.StackOverflowError中的异常(未知来源)
嗨,为什么我得到这个程序的stackoverflow异常?
我知道stackoverflow通常意味着你有一个无限循环,但是当我使用10000或其他一些较小的数字时,这种方法可以正常工作,而大数字突然变得无限大?
我正在学习如何将递归应用于数组.
例如,我通常以这种方式读取数组:
void read_array(int *a, int n){
int i;
for(i = 0; i < n; ++i)
scanf("%d", &a[i]);
return;
}
Run Code Online (Sandbox Code Playgroud)
我想以递归方式读取数组.我写了以下函数:
void read_array(int *a, int n){
int i = n - 1;
if (n < 0)
return;
else{
if(scanf("%d", &a[n - 1 - i]) == 1){
read_array(a, n - 1);
return;
}
}
}
Run Code Online (Sandbox Code Playgroud)
它编译,但在运行时会产生分段错误错误.它让我感到困惑,因为函数考虑了一个0应该阻止它的基础案例.
我正在审查Scala的考试,并试图找出我错过的这个测验问题.我理解尾递归是"最后一次调用本身",但我对其中一些代码片段之间的区别感到困惑.为什么这被认为是尾递归,
def f(x: Int): Int = {
if (x % 2 == 0) {1}
else {f(x + 1)}
Run Code Online (Sandbox Code Playgroud)
但是,这不是吗?
def f(x: Int): Int = {
if (x % 2 == 0) {1}
else {1 + f(x + 1)}
Run Code Online (Sandbox Code Playgroud)
为函数添加1究竟是什么限制了它是尾递归?如果这是一个愚蠢的问题,我很抱歉,我没有看到数字的影响,并希望巩固我的理解.完全能够识别尾递归的任何其他指针也会很棒!
There is this problem on LeetCode that I can not get to work in C/C++
The idea is to reverse an array in its place (using no other additional array) using recursion.
The link is : https://leetcode.com/explore/learn/card/recursion-i/250/principle-of-recursion/1440/
The solution is done in Java or Python.
I tried implementing the solution in C but I always get the original array, my code is as follows:
void reverseString(char* s, int sSize){
if(!s)
return;
reverseString(s+1,sSize-1);
s[sSize] = *s;
}
Run Code Online (Sandbox Code Playgroud)
There is something I …
我无法将我的大脑围绕尾递归,特别是在 ocaml 中,也解释了为什么人们在最后调用“in”函数。PS我说的是最基本的尾递归函数。
在我的练习中,我必须决定函数是什么类型的递归。
我们必须从线性递归、尾递归和保护递归中进行选择,但我不太明白后两者之间的区别。
有人可以解释一下保护递归和尾递归之间的区别吗?
我们要区分的功能供参考:
pow2 0 = 1
pow2 n = 2 * pow2 (n-1)
factAux r i n
| i <= n = factAux (i * r) (i + 1) n | otherwise = r
factorial = factAux 1 1
init [x] = []
init (x:xs) = x : init xs
binom n 0 = 1
binom n k
| n == k = 1
| otherwise = binom (n - 1) k + binom (n - 1) (k …Run Code Online (Sandbox Code Playgroud) 作为练习,我使用python中的递归实现了map函数,如下所示:
#map function that applies the function f on every element of list l and returns the new list
def map(l,f):
if l == []:
return []
else:
return [f(l[0])] + map(l[1:],f)
Run Code Online (Sandbox Code Playgroud)
我知道python不支持尾递归优化这一事实,但我如何以尾递归方式编写相同的函数?
请帮助谢谢
以下代码不返回None,而是一些神秘的" main _.link_list对象":
class link_list(object):
"""a link list class"""
def __init__(self, list):
super(link_list, self).__init__()
pdb.set_trace()
if list==[]:
return None
else:
self.value = list[0]
self.next = link_list(list[1:len(list)])
Run Code Online (Sandbox Code Playgroud)
测试代码打印'False':
l=link_list([1,2,3])
print l.next.next.next==None
Run Code Online (Sandbox Code Playgroud)
为什么?
我正在学习尾递归,在解决这个问题之前,我想对代码片段是否尾递归进行是/否的回答。
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)