1.爬楼地
爬20个台阶的爬法:=f(19)+f'(18)
经典斐波拉契数列问题
public class demo4 {//爬楼梯问题public static void main(String[] args) {System.out.println(getSum(20));}public static int getSum(int n) {if (n == 1)return 1;if (n == 2)return 2;return getSum(n - 1) + getSum(n - 2);}
}
2.按照要求排序问题
package lx;import java.util.Arrays;
import java.util.Comparator;public class demo1 {public static void main(String[] args) {Girlfriend g1 = new Girlfriend("zhangsan", 18, 90);Girlfriend g2 = new Girlfriend("lisi", 19, 100);Girlfriend g3 = new Girlfriend("wangwu", 20, 110);//定义数组存储对象Girlfriend[] arr = {g1, g2, g3};// Arrays.sort(arr, new Comparator<Girlfriend>() {/* public int compare(Girlfriend o1, Girlfriend o2) {//按照年龄的大小进行比较,年龄一样,按照身高比较,身高一样按照姓名的字母比较double tmp = o1.getAge() - o2.getAge();tmp = tmp == 0 ? o1.getHeight() - o2.getHeight() : tmp;tmp = tmp == 0 ? o1.getName().compareTo(o2.getName()) : tmp;if (tmp < 0) {return -1;} else if (tmp > 0) {return 1;} elsereturn 0;}});*///lambda表达式//()->{}//():对应抽象方法的形参//{}:方法体Arrays.sort(arr, (o1, o2) -> {//按照年龄的大小进行比较,年龄一样,按照身高比较,身高一样按照姓名的字母比较double tmp = o1.getAge() - o2.getAge();tmp = tmp == 0 ? o1.getHeight() - o2.getHeight() : tmp;tmp = tmp == 0 ? o1.getName().compareTo(o2.getName()) : tmp;if (tmp < 0) {return -1;} else if (tmp > 0) {return 1;} elsereturn 0;});System.out.println(Arrays.toString(arr));}
}
女朋友类
package lx;public class Girlfriend {private String name;private int age;private int height;public Girlfriend(String name, int age, int height) {this.name = name;this.age = age;this.height = height;}public Girlfriend() {}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public int getHeight() {return height;}public void setHeight(int height) {this.height = height;}@Overridepublic String toString() {return "Girlfriend{" +"name='" + name + '\'' +", age=" + age +", height=" + height +'}';}
}
3.兔子问题(斐波拉契数列)
package lx;public class demo2 {public static void main(String[] args) {//斐波那契数列int[] arr = new int[12];arr[0] = 1;arr[1] = 1;for (int i = 2; i < arr.length; i++) {arr[i] = arr[i - 1] + arr[i - 2];}System.out.println(arr[11]);}
}
4.猴子吃桃问题
package lx;public class demo3 {public static void main(String[] args) {//猴子偷桃问题/*有一堆桃子,猴子第一天吃了其实一半,并且多吃了一个!以后每天猴子都吃当前剩下来的一半,然后多吃一个第10天的时候(还没有吃),发现就剩下一个桃子,请问最初总共多少个桃子day10 1;day9=(day10+1)*2 4day8=(day9+1)*2 10每一天的桃子的数量都是后一天数量+1,*2*///method01();/*1.出口day==10 剩下一个2. 规律每一天的桃子的数量都是后一天数量+1,*2*/System.out.println(getCount(1));}public static int getCount(int day) {if (day <= 0 || day >= 11) {System.out.println("当前时间错误");return -1;}//出口if (day == 10) {return 1;} else {// 每一天的桃子的数量都是后一天数量+1,*2return (getCount(day + 1) + 1) * 2;}}private static void method01() {int day = 0;int x = 1;int y = 0;for (day = 9; day >= 1; day--) {y = (x + 1) * 2;x = y;}System.out.println(y);}
}