Problem Description
Are you excited when you see the title "AC" ? If the answer is YES , AC it ;
You must learn these two combination formulas in the school . If you have forgotten it , see the picture.
Now I will give you n and m , and your task is to calculate the answer .
You must learn these two combination formulas in the school . If you have forgotten it , see the picture.
Now I will give you n and m , and your task is to calculate the answer .
Input
In the first line , there is a integer T indicates the number of test cases.
Then T cases follows in the T lines.
Each case contains a character 'A' or 'C', two integers represent n and m. (1<=n,m<=10)
Then T cases follows in the T lines.
Each case contains a character 'A' or 'C', two integers represent n and m. (1<=n,m<=10)
Output
For each case , if the character is 'A' , calculate A(m,n),and if the character is 'C' , calculate C(m,n).
And print the answer in a single line.
And print the answer in a single line.
Sample Input
2 A 10 10 C 4 2
Sample Output
3628800 6
思路:首先计算1到10之间的阶乘表,计算时直接查表即可
代码如下
import java.io.FileInputStream;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
public class Main implements Runnable
{
private static final boolean DEBUG = false;
private Scanner cin;
private PrintWriter cout;
private int n, m;
private static final int N = 11;
private static final int[] fact = new int[N];
private String str;
private void init()
{
try {
if (DEBUG) {
cin = new Scanner(new InputStreamReader(new FileInputStream("f:\\OJ\\uva_in.txt")));
} else {
cin = new Scanner(new InputStreamReader(System.in));
}
cout = new PrintWriter(new OutputStreamWriter(System.out));
fact[0] = 1;
for (int i = 1; i < N; i++) {
fact[i] = i * fact[i - 1];
}
} catch (Exception e) {
e.printStackTrace();
}
}
private boolean input()
{
str = cin.next();
n = cin.nextInt();
m = cin.nextInt();
return true;
}
private void solve()
{
int ans = 0;
if (str.compareTo("A") == 0) {
ans = fact[n] / fact[n - m];
} else {
ans = fact[n] / (fact[n - m] * fact[m]);
}
cout.println(ans);
cout.flush();
}
@Override
public void run()
{
init();
int t = cin.nextInt();
while (t-- > 0)
{
input();
solve();
}
}
public static void main(String[] args)
{
// TODO code application logic here
new Thread(new Main()).start();
}
}