常见的算法题,难度从简单到困难,可以用来开拓下思维。
- 根据年龄,身高,姓名排序,前一个相等就用后一个进行排序
javabean基础类
java
package com.gaofeng.demo09;
public class GirlFriend {
private String name;
private Integer age;
private Double height;
public GirlFriend() {
}
public GirlFriend(String name, Integer age, Double height) {
this.name = name;
this.age = age;
this.height = height;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public Integer getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(Integer age) {
this.age = age;
}
/**
* 获取
* @return height
*/
public Double getHeight() {
return height;
}
/**
* 设置
* @param height
*/
public void setHeight(Double height) {
this.height = height;
}
public String toString() {
return "GirlFriend{name = " + name + ", age = " + age + ", height = " + height + "}";
}
}
java
package com.gaofeng.demo09;
import java.util.Arrays;
import java.util.Comparator;
public class demo01 {
public static void main(String[] args) {
//
GirlFriend gf1 = new GirlFriend("xiaoshishi", 21, 1.67);
GirlFriend gf2 = new GirlFriend("xiaodandan", 19, 1.70);
GirlFriend gf3 = new GirlFriend("xiaohuihui", 20, 1.68);
GirlFriend[] arr = {gf1, gf2, gf3};
/*Arrays.sort(arr, new Comparator<GirlFriend>() {
@Override
public int compare(GirlFriend o1, GirlFriend o2) {
double i = o1.getAge() - o2.getAge();
i = i == 0 ? o1.getHeight() - o2.getHeight() : i;
i = i == 0 ? o1.getName().compareTo(o2.getName()):i;
if(i > 0){
return 1;
}else if(i < 0){
return -1;
}else{
return 0;
}
}
});*/
Arrays.sort(arr, (o1,o2) -> {
double i = o1.getAge() - o2.getAge();
i = i == 0 ? o1.getHeight() - o2.getHeight() : i;
i = i == 0 ? o1.getName().compareTo(o2.getName()):i;
if(i > 0){
return 1;
}else if(i < 0){
return -1;
}else{
return 0;
}
});
System.out.println(Arrays.toString(arr));
}
}
- 斐波拉契数列
java
package com.gaofeng.demo09;
import java.util.Arrays;
public class Demo02 {
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(Arrays.toString(arr));*/
int num = fibonacci(12);
System.out.println(num); // 144
}
// 递归解决
public static int fibonacci(int n) {
if (n == 1 || n == 2) {
return 1;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
}
- 猴子吃桃问题,猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个,第二天早上又将剩下的桃子吃掉一半,又多吃了一个,直到第10天,只剩下1个,共摘了多少个桃子?
java
package com.gaofeng.demo09;
public class Demo03 {
public static void main(String[] args) {
// 出口 day == 10 1
System.out.println(getCount(1)); //1534
}
public static int getCount(int day){
if(day > 10 || day < 1){
System.out.println("输入错误");
}
if(day == 10){
return 1;
}else{
return (getCount(day + 1) + 1)*2;
}
}
}
- 爬楼梯问题,有20阶楼梯,每次只能爬1阶或者2阶,有多少种走法
java
package com.gaofeng.demo09;
public class Demo04 {
public static void main(String[] args) {
System.out.println(getCount(20)); //10946
}
// 爬楼梯
public static int getCount(int step){
if(step == 1){
return 1;
}else if(step == 2){
return 2;
}else{
return getCount(step - 1) + getCount(step - 2);
}
}
}
- 爬楼梯,有时候可以一个台阶,有时候可以两个台阶,有时候可以三个台阶,总共有20个台阶,有多少种走法
java
package com.gaofeng.demo09;
public class Demo05 {
public static void main(String[] args) {
int count = getCount(20);
System.out.println(count); //121415
}
// 爬楼梯,有时候可以一个台阶,有时候可以两个台阶,有时候可以三个台阶,总共有20个台阶,有多少种走法
public static int getCount(int step){
if(step == 1){
return 1;
}else if(step == 2){
return 2;
}else if(step == 3){
return 4;
}else{
return getCount(step - 1) + getCount(step - 2) + getCount(step - 3);
}
}
}