Java基础:经典算法题

1.算法题一 

Java基础:经典算法题_第1张图片

Girlfriend Nini = new Girlfriend("Nini", 22, 170);
        Girlfriend Lili = new Girlfriend("Lili", 22, 170);
        Girlfriend Vinida = new Girlfriend("Vinida", 22, 170);

        Girlfriend[] girls = {Nini, Lili, Vinida};

        Arrays.sort(girls, new Comparator() {
            @Override
            public int compare(Girlfriend o1, Girlfriend o2) {
                int age = o1.getAge() - o2.getAge();
                long height = o1.getHeight() - o2.getHeight();
                // 笨方法
                int name = o1.getName().charAt(0) - o2.getName().charAt(0);
                // 直接调用字符串方法来比较
                int name2 = o1.getName().compareTo(o2.getName());
                if (age != 0){
                    return age;
                }else {
                    if (height != 0){
                        return (int) height;
                    }else {
                        return name2;
                    }

                }
            }
        });
        System.out.println(Arrays.toString(girls));

2.算法题二

public static void main(String[] args) {
        // 需要递归
        // 需要兔子的数量(几对), 需要还剩几个月
        // 输出兔子的对数
        int maturerabbit = 0;
        int newrabbit = 1;
        int mouth = 11;
        int rabbitnum = countRabbit(maturerabbit, newrabbit, mouth);
        System.out.println(rabbitnum);
        // 也可以找出规律
        // 假如第一个月成熟的为x, 不成熟的为y, 总数为x+y
        // 第二个月成熟的为x+y, 不成熟的为x, 总数为2x+y
        // 第三个月成熟的为2x+y, 不成熟的为x+y, 总数为3x+2y
        // 所以第三个月就为前两个月数量和
        int mouth2 = 12;
        int rabbit2 = countRabbit2(mouth2);
        System.out.println(rabbit2);
    }

    private static int countRabbit2(int mouth) {
        if (mouth == 1 || mouth == 2){
            return 1;
        }
        return countRabbit2(mouth - 1) + countRabbit2(mouth - 2);
    }

    private static int countRabbit(int maturerabbit, int newrabbit, int mouth) {
        // each mouth
        if (mouth < 1){
            return maturerabbit + newrabbit;
        }
        int temp = maturerabbit;
        maturerabbit = maturerabbit + newrabbit;
        newrabbit = temp;
        mouth = mouth - 1;
        return countRabbit(maturerabbit, newrabbit, mouth);
    }

3.算法题三

public static void main(String[] args) {
        // 假如一开始是x个桃子
        // 第一天吃了x/2 + 1个, 还剩x/2 - 1个
        // 以此类推, 假如第十天只剩y个, 那么第九天剩 (y + 1)*2个
        int day = 9;
        int remain = 1;
        int peachnum = countpeach(day, remain);
        System.out.println(peachnum);

        // 2
        // 后一天的数量为前一天数量+1再*2
        int day2 = 1;
        int peachnum2 = countpeach2(day2);
        System.out.println(peachnum2);
    }

    private static int countpeach(int day, int remain) {
        if (day < 1){
            return remain;
        }
        return countpeach(day - 1, (remain + 1)*2);
    }
    private static int countpeach2(int day) {
        // 出口
        if (day == 10){
            // 第十天数量为1
            return 1;
        }
        return (countpeach2(day + 1) + 1) * 2;
    }

4.算法题四

Java基础:经典算法题_第2张图片

public static void main(String[] args) {
        // 根据规律, 该阶梯的爬法为前两阶爬法的和
        // 因为前一阶的爬法后加个1, 前二阶爬法加个2就能构成该阶爬法
        int step = 20;
        int countclimb = countclimb(step);
        System.out.println(countclimb);
    }

    private static int countclimb(int step) {
        if (step == 1){
            return 1;
        }
        if (step == 2){
            return 2;
        }
        return countclimb(step - 1) + countclimb(step - 2);
    }

你可能感兴趣的:(java,开发语言,数据结构)