算法基础_递归_给定m个A,n个B,一共有多少种排列

问题描述:

给定m个A,n个B,一共有多少种排列

解题源代码:

/**
 *     给定m个A,n个B,问一共有多少种排列
 * @author Administrator
 *
 */
public class Demo06 {
    public static int f(int m,int n) {
        if(m==0||n==0)return 1;
        return f(m-1,n)+f(m,n-1);
    }
    
    public static void main(String[] args) {
        System.out.println(f(3,2));
    }
}

解题思路:

同样,递归就是找到规律,然后直接return即可

此处的规律是:在每一个位置上都有两种可能,所以,在第一层进行分支即可,然后设置到底的条件

希望能给大家带来帮助

以上

原文地址:https://www.cnblogs.com/lavender-pansy/p/10533046.html