递推递归练习F计算组合数

来源:互联网 发布:求婚大作战 知乎 编辑:程序博客网 时间:2024/05/31 19:03

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

2 1
3 2
4 0

Sample Output

2
3
1
水题,照着题目翻译就行。
#include<iostream>using namespace std;int c(int a,int b){if(b==0)return 1;else if(a==1)return 1;       else if(a==b)return 1;              else return c(a-1,b-1)+c(a-1,b);}int main(){int n,m,N;cin>>N;for(int i=0;i<N;i++){cin>>n>>m;cout<<c(n,m)<<endl;}return 0;}

0 0
原创粉丝点击