递归题--吃苹果问题 数组中最大值

                                   吃苹果问题

每天吃三分之一再加一个,到最后一天只有一个。

public static void main(String[] args) {
        System.out.println(getNum(3));
    }
    static int dp = 0;

    public static int getNum(int n) {
        if (n == 1) {
            return 1;
        }
        dp = 1 + getNum(n - (n / 3 + 1));
        return dp;
    }

                                 数组中最大值

static int res = Integer.MIN_VALUE;

    public static int getMax(int[] nums, int n) {
        if (n == 0) {
            return res;
        }
        res = Math.max(nums[n], getMax(nums, n - 1));
        return res;
    }
发布了416 篇原创文章 · 获赞 19 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/weixin_41563161/article/details/105134640