简体   繁体   English

二项式系数数组

[英]Array of binomial coefficients

So, I have implemented the binomial coefficient所以,我已经实现了二项式系数

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));
}

And it works, but where I'm stuck is just that I need to add the coefficient array for two given numbers.它有效,但我遇到的问题只是我需要为两个给定数字添加系数数组 So If n is 5 and k is 3 .所以如果n5并且k3 The coefficient array will display: 1 5 10 10 .系数数组将显示: 1 5 10 10 Any ideas?有任何想法吗?

Don't call recursive code in a for loop.不要在 for 循环中调用递归代码。 That adds a stupid amount of redundancy.这增加了大量的冗余。

Pass the array as a parameter to the recursive function from main.将数组作为参数传递给 main 中的递归函数。 Arrays are passed by reference in java. 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;
}

All you need to do is put your expression in a loop and hold n constant.您需要做的就是将表达式放入循环中并保持n不变。

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

You can store these values in an array if you like.如果您愿意,可以将这些值存储在数组中。 There is no need to make your method any more complicated.没有必要让你的方法变得更复杂。

If want to put it in an array, here is one easy way to do it.如果想把它放在一个数组中,这是一种简单的方法。

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

You can create two iterative methods: one returns a 2d array containing Pascal's triangle , and the second returns the base of that triangle.您可以创建两种迭代方法:一种返回包含Pascal 三角形的二维数组,第二种返回该三角形的 It is more useful for clarity.为了清晰起见,它更有用。

Output:输出:

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]

Code:代码:

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));
}

See also: Convert negative index to positive index in an array (Trinomial Triangle)另请参阅:将数组中的负索引转换为正索引(三角三角形)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM