小编Bul*_*aur的帖子

尾递归函数仍然在 Java 中炸毁堆栈

我正在尝试实现尾递归阶乘计算器,但仍然出现堆栈溢出。谁能帮我找出原因?

  • 我读过 Java 8 支持尾调用优化,但我想我一定没有正确实现它。
  • 我已经读到可以使用 lambda 表达式。我不确定我是否完全理解这个概念,但我仍在阅读。
  • 我只是在寻找有关如何使用真正的尾调用优化、lambda 表达式或我可以使用的任何建议。

代码:

package factorielRecursiveTerminale;

import java.math.BigInteger;
import java.util.Scanner; 

public class factorielRecursiveTerminale {  
    
    public static BigInteger factoriel(BigInteger n, BigInteger m) {
        if (n.compareTo(BigInteger.ZERO) < 1) return m;             
        return factoriel(n.subtract(BigInteger.ONE), n.multiply(m));
    }                                                               
                                                                
    public static BigInteger fact(int n) {  //convertir l'entree en BigInteger et lancer la recursion
        if(n < 0) {
            return BigInteger.valueOf(-1);
        }
        BigInteger b = BigInteger.valueOf(n);
        return factoriel(b, BigInteger.ONE);
    }

    public static void runBigFact() {                       //gestion des erreurs + boucle d'entree de …
Run Code Online (Sandbox Code Playgroud)

java tail-recursion biginteger factorial

2
推荐指数
1
解决办法
168
查看次数

想从 C++ 程序中运行 PowerShell 代码

我希望能够从我的 C++ 程序中运行 30 行 PowerShell 脚本。我听说这是一个糟糕的主意,我不在乎。我还是想知道怎么做。

我只想直接在 C++ 程序中编码,我不想从外部调用 PowerShell 脚本。有没有办法做到这一点?如果不可能,就说不。

例如

void runPScode() {

//command to tell compiler the following is PowerShell code
//a bunch of PowerShell code

}
Run Code Online (Sandbox Code Playgroud)

谢谢!

我一直在寻找执行此操作的命令并阅读了几个“类似”的问题。

c++ powershell

0
推荐指数
1
解决办法
2627
查看次数

标签 统计

biginteger ×1

c++ ×1

factorial ×1

java ×1

powershell ×1

tail-recursion ×1