#P1434. [递归入门]取球1

[递归入门]取球1

Description

从M个互不相同的球中取N个,一共由多少种取法?

参考答案见提示

Input Format

两个数,M和N。(1 < M,N < 20)## Output Format

取法总数

5 3​
8​

Hint

#include <iostream>
using namespace std;
int f(int m,int n){
    if(n==0)
        return 1;
    if(m==n)
        return 1;
    return f(m,n-1)+f(m-1,n-1);
}
int main(){
    int m,n;
    cin>>m>>n;
    cout<< f(m,n);
    return 0;
}

法2

#include <iostream>
using namespace std;
int tot = 0;
void f(int m,int n){
    if(n==0){
        tot++;
        return ;
    }
    if(m==n){
        tot++;
        return ;
    }
    f(m,n-1);
    f(m-1,n-1);
}
int main(){
    int m,n;
    cin>>m>>n;
    f(m,n);
    cout<< tot;
    return 0;
}

Source

递归