package dynamicProgramming; /* * 用动态规划算法计算C(n,k) * 输入:一对非负整数n>=k>=0 * 输出:C(n,k)的值 */ import java.util.*; public class Binomial { int C[][]; int Computing(int n,int k){ C = new int[n+1][n+1]; for(int i=0;i<=n;i++){ for(int j=0;j<=Math.min(i, k);j++){ if(j==0||j==i){ C[i][j] = 1; } else{ C[i][j] = C[i-1][j-1]+C[i-1][j]; } } } return C[n][k]; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("请输入n:"); int n = in.nextInt(); System.out.println("请输入k(0<=k<=n):"); int k = in.nextInt(); System.out.println("C["+n+","+k+"] = "+new Binomial().Computing(n, k)); } }