递归

方法的递归

什么情况下使用递归?      

  • 一个复杂问题可以转换为与原问题相似的规模较小的问题

递归的注意点

  • 递归出口:递归一定要有一个出口,否则会出现内存溢出

例题1
        一个工厂生产汽车,1月生产了10000量,二月
        生产的汽车是一月产量的1倍减去3000量,以后
        每个月的产量都以此类推,问12月工厂生产
        汽车多少量?

public class Recursion {
    public static int getCount(int month){
        if (month==1){
            return 10000;
        }else{
            return getCount(month-1)*2-3000;
        }
    }

    public static void main(String[] args) {
        System.out.println(Recursion.getCount(12));
    }
}
posted @ 2021-07-19 10:05  小盆友在学习  阅读(84)  评论(0)    收藏  举报