【问题标题】:Why my Pascal's triangle code doesn't work?为什么我的帕斯卡三角码不起作用?
【发布时间】:2019-12-22 16:14:30
【问题描述】:

我一直在尝试使用组合公式编写帕斯卡三角形,但它无法正常工作,我不确定问题出在哪里?

这是输入:

public class Probs {
    public static int fact(int n) {
        int f;
        for (f = 1; n > 1; n--) {
            f *= n;
        }
        return f;
    }

    public static int comb(int i, int j) {
        return fact(i) / fact(i - j) * fact(j);
    }

    public static void main(String[] args) {
        int n = 5;
        int i;
        int j;
        for (i = 0; i < n; i++) {
            for (j = 0; j < n - i; j++) {
                System.out.print(" ");
            }
            for (j = 0; j <= i; j++) {
                System.out.print(" " + comb(i, j));
            }
            System.out.println();
        }
    }
}

输出:

    1
   1 1
  1 2 4
 1 3 12 36
1 4 24 144 576

你能以一种对初学者友好的方式解释我为什么吗?

【问题讨论】:

    标签: java algorithm recursion pascals-triangle


    【解决方案1】:

    您需要在comb() 中的操作周围添加括号以获得正确的优先级。

    /* 的优先级相同,所以当你写的时候

    返回事实(i)/事实(i-j)*事实(j);

    这个表达式其实等价于

    返回 (fact(i)/fact(i-j)) * fact(j);

    这不是你真正想要的......

    通过在分母中的乘积周围添加括号来修复它:

    返回事实(i)/事实(i-j)*事实(j)

    【讨论】:

      【解决方案2】:

      检查您的公式以获得阶乘。应该是:

      阶乘(i) / (阶乘(i - j) * 阶乘(j))

      使用 streams,您的代码可能如下所示:

      public static int factorial(int n) {
          return IntStream.rangeClosed(2, n).reduce((a, b) -> a * b).orElse(1);
      }
      
      public static int[][] pascalsTriangle(int n) {
          return IntStream.range(0, n)
                  .mapToObj(i -> IntStream.range(0, i + 1)
                          .map(j -> factorial(i) / (factorial(i - j) * factorial(j)))
                          .toArray())
                  .toArray(int[][]::new);
      }
      
      public static void main(String[] args) {
          int n = 10;
          int[][] arr = pascalsTriangle(n);
          pyramidOutput(arr);
      }
      
      public static void pyramidOutput(int[][] arr) {
          String[] output = Arrays.stream(arr)
                  .map(row -> Arrays.stream(row).mapToObj(String::valueOf)
                          .collect(Collectors.joining("  ")))
                  .toArray(String[]::new);
      
          int max = Arrays.stream(output)
                  .max(Comparator.comparing(String::length))
                  .orElse("").length();
      
          Arrays.stream(output)
                  .map(row -> " ".repeat((max - row.length()) / 2) + row)
                  .forEach(System.out::println);
      }
      

      输出:

                       1
                      1  1
                    1  2  1
                   1  3  3  1
                 1  4  6  4  1
               1  5  10  10  5  1
             1  6  15  20  15  6  1
           1  7  21  35  35  21  7  1
         1  8  28  56  70  56  28  8  1
      1  9  36  84  126  126  84  36  9  1
      

      另见:How do I make this into a Pascal's triangle instead of a Right triangle?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-28
        • 2012-07-13
        相关资源
        最近更新 更多