【问题标题】:How Can Print a Visualization to Factorial of a Number with Recursion如何使用递归打印数字的阶乘可视化
【发布时间】:2018-05-16 02:47:09
【问题描述】:

我知道如何计算数字的阶乘,但我想在方法调用自身时显示内存中发生的情况。所以使用递归的阶乘是休闲

public static long factorial(int n) { 
    if (n == 1) return 1; 
    return n * factorial(n-1); 
} 

我想实现以下目标

factorial(5) 
   factorial(4) 
      factorial(3) 
         factorial(2) 
            factorial(1) 
               return 1 
            return 2*1 = 2 
         return 3*2 = 6 
      return 4*6 = 24 
   return 5*24 = 120

我已经到了这一点...我无法显示方法的递归返回(第二部分)

public static long factorial(int n) {   
    System.out.println("factorial("+n+")");
    if (n == 1) {
        System.out.println("return 1");
        return 1; 
    }       
    return n * factorial(n-1);      
} 

【问题讨论】:

    标签: java recursion factorial


    【解决方案1】:

    在返回之前尝试打印:

    public static long factorial(int n) {   
        System.out.println("factorial("+n+")");
        if (n <= 1) { // factorial(0) = factorial(1) = 1
            System.out.println("return 1");
            return 1; 
        }
        long fac = factorial(n-1);
        System.out.printf("return %d * %d = %d%n", n, fac, n * fac);
        return n * fac;      
    } 
    

    要获得每行 n 个空格,您可以添加一个辅助函数,该函数接受递归的当前“深度”并相应地打印 n 个空格。

    // public function visible to the world
    public static long factorial(int n) {   
        return factorial(5, 0);
    } 
    
    // helper function that takes in the current depth of 
    // the recursion
    private static long factorial(int n, int depth) {
        String spaces = repeat(' ', depth);
        System.out.print(spaces);
        System.out.println("factorial("+n+")");
        if (n <= 1) { // factorial(0) = factorial(1) = 1
            System.out.println(spaces + " return 1");
            return 1; 
        }
    
        long fac = factorial(n-1, depth + 1);
        System.out.print(spaces);
        System.out.printf("return %d * %d = %d%n", n, fac, n * fac);
        return n * fac;      
    }
    
    // helper function to create a String by repeating
    // char c, n times.
    private static String repeat(char c, int times) {
        char[] sequence = new char[times];
        Arrays.fill(sequence, c);
        return new String(sequence);
    }
    

    【讨论】:

    • 如何在每个函数调用中添加空格,如输出所示?
    • @miatech 我添加了第二个实现,我将空格考虑在内
    【解决方案2】:

    您可以使用 try{}finally{} 块“在”返回后执行某些操作

    public static long factorial(int n) {
        try {
            System.out.println("factorial(" + n + ")");
            if (n == 1) {
                System.out.println("return 1");
                return 1;
            }
            return n * factorial(n - 1);
        } finally {
            System.out.println("return " + n);
        }
    }
    

    【讨论】:

    • 如果n == 1 会不会打印两次?
    • 是的,检查确实应该在最后。与打印相关的所有内容都可能在其中
    • 完成工作,没有不良副作用,完全滥用try...catch...finally机制。换句话说,a sweet hack (;->).
    • @KevinAnderson 作为一个特别邪恶的奖励,finally 中的return 会覆盖退出受保护块的那个。简而言之,finally 总是赢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    • 1970-01-01
    • 2019-08-06
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    相关资源
    最近更新 更多