【java练习】SDUT 1586 计算组合数

Problem Description

计算组合数。C(n,m),表示从n个数中选择m个的组合数。
计算公式如下:
若:m=0,C(n,m)=1
否则, 若 n=1,C(n,m)=1
             否则,若m=n,C(n,m)=1
                         否则 C(n,m) = C(n-1,m-1) + C(n-1,m).

Input

第一行是正整数N,表示有N组要求的组合数。接下来N行,每行两个整数n,m (0 <= m <= n <= 20)。

Output

输出N行。每行输出一个整数表示C(n,m)。

Sample Input

3
2 1
3 2
4 0

Sample Output

2
3
1
import java.util.Scanner;

class lei {
	int f(int n, int m) {
		int sum;
		if (m == 0 || n == 1 || m == n)
			sum = 1;
		else
			sum = f(n - 1, m - 1) + f(n - 1, m);
		return sum;
	}
}

public class Main {

	public static void main(String[] args) {
		Scanner s = new Scanner(System.in);
		int c = s.nextInt();
		while (c>0) {c--;
			int n = s.nextInt();
			int m = s.nextInt();
			lei t = new lei();//首先在lei中建立对象t
			int summ = t.f(n, m);//通过实例对象调用类的方法
			System.out.println(summ);
		}
		s.close();
	}
}

猜你喜欢

转载自blog.csdn.net/flyf000/article/details/83013024