【问题标题】:Array of binomial coefficients二项式系数数组
【发布时间】:2019-12-07 16:21:59
【问题描述】:

所以,我已经实现了二项式系数

public static int binomial(int n, int k) {
    if (k == 0)
        return 1;
    else if (k > n - k)
        return binomial(n, n - k);
    else
        return binomial(n - 1, k - 1) * n / k;
}

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);

    System.out.println("Insert n: ");
    int n = scan.nextInt();

    System.out.println("Insert k: ");
    int k = scan.nextInt();

    System.out.println("Result: " + binomial(n, k));
}

它有效,但我卡住的地方只是我需要为两个给定数字添加 系数数组。所以如果n5 并且k3。系数数组将显示:1 5 10 10。有什么想法吗?

【问题讨论】:

    标签: java arrays pascals-triangle binomial-coefficients


    【解决方案1】:

    不要在 for 循环中调用递归代码。这增加了愚蠢的冗余。

    将数组作为参数从 main 传递给递归函数。数组在java中通过引用传递。

    public static int binomial(int n, int k, int[] coefficient) {
        int ret;
        if (k == 0) {
            ret = 1;
        } else if (k > n - k) {
            ret = binomial(n, n - k, coefficient);
        } else {
            ret = binomial(n - 1, k - 1, coefficient) * n / k;
        }
        coefficient[k] = ret;
        return ret;
    }
    

    【讨论】:

      【解决方案2】:

      您需要做的就是将您的表达式放入一个循环中并保持n 不变。

      for (int k = 0; k <= n; k++) {
          System.out.print(binomial(n, k) + " ");
      }
      

      如果您愿意,您可以将这些值存储在一个数组中。没有必要让你的方法变得更复杂。

      如果想把它放在一个数组中,这里有一种简单的方法。

      int coefs[] = IntStream.rangeClosed(0, n).map(k -> binomial(n, k)).toArray();
      
      coefs[] = [1, 5, 10, 10, 5, 1]
      

      【讨论】:

        【解决方案3】:

        您可以创建两个迭代 方法:一个返回包含Pascal 三角形 的二维数组,第二个返回该三角形的。它更有助于清晰。

        输出:

        Insert n:
        6
        Pascal's triangle:
        [1, 1, 1, 1, 1, 1]
        [1, 2, 3, 4, 5]
        [1, 3, 6, 10]
        [1, 4, 10]
        [1, 5]
        [1]
        Binomial coefficients:
        [1, 5, 10, 10, 5, 1]
        

        代码:

        public static int[][] binomialTriangle(int n) {
            // an array of 'n' rows
            int[][] arr = new int[n][];
            // iterate over the rows of the array
            IntStream.range(0, n).forEach(i -> {
                // a row of 'n-i' elements
                arr[i] = new int[n - i];
                // iterate over the columns of the array
                IntStream.range(0, n - i).forEach(j -> {
                    if (i == 0 || j == 0)
                        // first row and column
                        // are filled with ones
                        arr[i][j] = 1;
                    else
                        // all other elements are the sum of the
                        // previous element in the row and column
                        arr[i][j] = arr[i][j - 1] + arr[i - 1][j];
                });
            });
            return arr;
        }
        
        public static int[] binomialCoefficient(int[][] triangle) {
            return Arrays.stream(triangle)
                    // the last element in the row
                    .mapToInt(row -> row[row.length - 1])
                    .toArray();
        }
        
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            System.out.println("Insert n:");
            int n = scan.nextInt();
        
            System.out.println("Pascal's triangle:");
            int[][] arr = binomialTriangle(n);
            Arrays.stream(arr).map(Arrays::toString).forEach(System.out::println);
        
            int[] base = binomialCoefficient(arr);
            System.out.println("Binomial coefficients:");
            System.out.println(Arrays.toString(base));
        }
        

        另见:Convert negative index to positive index in an array (Trinomial Triangle)

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-04-23
          • 1970-01-01
          • 2023-02-20
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多