计算组合数

来源:互联网 发布:ubuntu翻墙上google 编辑:程序博客网 时间:2024/05/22 07:07

计算组合数

Time Limit: 1000MS Memory Limit: 32768KB
SubmitStatistic

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)。

Example Input

32 13 24 0

Example Output

231

Hint


Author

 
#include <stdio.h>
#include <math.h>
int c(int n,int m)
{
    int l;
    if(m==0)l=1;
    else if(n==1)l=1;
    else if(n==m)l=1;
    else l=c(n-1,m-1)+c(n-1,m);
    return l;
}
int main()
{
    int t,i,n,m;
    scanf("%d",&t);
    for(i=1;i<=t;i++)
    {
        scanf("%d %d",&n,&m);
        printf("%d\n",c(n,m));
    }
    return 0;
}