本文主要是介绍ACMjava杨辉三角形与二项式定理递推实现与组合实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
package com.supermars.practice;import java.util.Scanner;public class 杨辉三角形与二项式定理递推 {static Scanner cin = new Scanner(System.in);static int C[][] = new int[1 << 7][1 << 7];public static void main(String[] args) {while (cin.hasNext()) {int n = cin.nextInt();C[0][0] = 1;for (int i = 1; i <= n; i++) {C[i][0] = 1;for (int j = 1; j <= n; j++) {C[i][j] = C[i - 1][j - 1] + C[i - 1][j];}}for (int i = 0; i <= n; i++) {for (int j = 0; j <= i; j++) {System.out.print(C[i][j] + " ");}System.out.println("");}}}
}
package com.supermars.practice;import java.util.Scanner;public class 杨辉三角形与二项式定理组合公式 {// C[k]=(n-k+1)/k*C[k-1]static Scanner cin = new Scanner(System.in);static long C[] = new long[1 << 7];public static void main(String[] args) {while (cin.hasNext()) {int n = cin.nextInt();C[0] = 1;for (int i = 1; i <= n; i++) {C[i] = C[i - 1] * (n - i + 1) / i;}print_C(n);}}private static void print_C(int n) {for (int i = 0; i <= n; i++) {System.out.print(C[i] + " ");}System.out.println("");}
}
这篇关于ACMjava杨辉三角形与二项式定理递推实现与组合实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!