Skip to content

Commit 47c40fb

Browse files
author
panzha
committed
田忌赛马、打家劫舍问题
1 parent 1ede11e commit 47c40fb

File tree

7 files changed

+274
-0
lines changed

7 files changed

+274
-0
lines changed

huawei/src/main/java/Main2.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main.java;
2+
3+
public class Main2 {
4+
public static void main(String[] args) {
5+
System.out.println("helloworld");
6+
}
7+
}

notes/ali/打家劫舍/README.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#正文:
2+
你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。
3+
4+
给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。
5+
6+
##示例 1:
7+
8+
###输入:
9+
[1,2,3,1]
10+
###输出:
11+
4
12+
###解释:
13+
偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。偷窃到的最高金额 = 1 + 3 = 4 。
14+
15+
###示例 2:
16+
17+
###输入:
18+
[2,7,9,3,1]
19+
###输出:
20+
12
21+
###解释:
22+
偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。
23+
24+
偷窃到的最高金额 = 2 + 9 + 1 = 12 。
25+
26+
##分析
27+
很明显的是动态规划题。对于动态规划题就是找他的动态规划方程,也就是找规律。
28+
###拿示例1讲解:
29+
[1,2,3,1]
30+
31+
我们会发现我们找的就是当前房屋金额和之前的间隔房屋最大金额与相邻房屋最大金额的最大值。
32+
33+
使用nums数组存储房屋的金额数,使用一个数组arr作为偷到该房屋最大金额数的存储,长度等于房屋数。对于arr[0]就是他自己的金额数为nums[0]=1,对于arr[1]呢,就是nums[0]和nums[1]的较大者,那么对于arr[i],我们能得到的最大金额是多少呢?就是它的金额数和间隔的房屋最大金额数与它相邻的房屋最大金额数。即:num[i]+arr[i-2]与arr[i-1]的较大者。
34+
35+
###动态规划方程为:
36+
```java
37+
arr[i] = Math.max(nums[i]+arr[i-2],arr[i-1]);
38+
```
39+
##代码
40+
41+
```java
42+
public int rob(int[] nums) {
43+
int len = nums.length;
44+
if (nums.length == 0) {
45+
return 0;
46+
} else if (nums .length == 1) {
47+
return nums[0];
48+
}
49+
int [] arr = new int[len];
50+
arr[0] = nums[0];
51+
arr[1] = nums[1] > nums[0] ? nums[1] : nums[0];
52+
for (int i = 2; i < len; i++) {
53+
arr[i] = Math.max(nums[i]+arr[i-2],arr[i-1]);
54+
}
55+
return arr[len-1];
56+
}
57+
```

notes/ali/田忌赛马/README.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#正文:
2+
##要求:
3+
输入两行数字,中间用空格分开,第一行代表田忌的马,第二行代表齐王的马,数字的大小代表马跑的速度,数字越大跑的越快,当两个数字相同时田忌不算赢,要求输出田忌最多能赢多少场
4+
###例 :
5+
```text
6+
输入:
7+
6 4 3 1
8+
8 5 4 2
9+
输出:
10+
2
11+
```
12+
###分析:
13+
这里的输入的是字符串,涉及到字符串转int型的数据类型转换,还要求按照其原理进行排兵布阵
14+
###代码:
15+
```java
16+
import java.util.ArrayList;
17+
import java.util.Collections;
18+
import java.util.List;
19+
import java.util.Scanner;
20+
21+
public class TianJi {
22+
public static void main(String[] args) {
23+
List<Integer> xiaotiantian = new ArrayList<Integer>();
24+
List<Integer> xiaoqiqi = new ArrayList<Integer>();
25+
Scanner sc = new Scanner(System.in);
26+
System.out.println("请输入一串数字数组,并用空格分隔(田忌的马):");
27+
String a = sc.nextLine();
28+
String[] arr1 = a.split(" ");
29+
for (String s : arr1) {
30+
xiaotiantian.add(Integer.parseInt(s));
31+
}
32+
System.out.println("请输入一串数字数组,并用空格分隔(齐王的马):");
33+
String b = sc.nextLine();
34+
String[] arr2 = b.split(" ");
35+
for (int i = 0; i < arr1.length; i++) {
36+
xiaoqiqi.add(Integer.parseInt(arr2[i]));
37+
}
38+
int n = xiaotiantian.size();
39+
boolean bLast = true;
40+
while (bLast) {
41+
if (n == 0) {
42+
break;
43+
}
44+
//处理数据
45+
Collections.sort(xiaotiantian);
46+
Collections.sort(xiaoqiqi);
47+
int i = 0, j = 0, x = n - 1, y = n - 1, cnt = 0;
48+
while (bLast) {
49+
//是否是最后一匹马
50+
if (x == i) {
51+
bLast = false;
52+
}
53+
if (xiaotiantian.get(x) > xiaoqiqi.get(y)) {//如果田忌当前最好的马可以胜齐王最好的马,那么比一场
54+
x--;
55+
y--;
56+
cnt++;
57+
} else if (xiaotiantian.get(i) > xiaoqiqi.get(j)) {//如果田忌当前最差的马可以胜齐王最差的马,那么比一场
58+
i++;
59+
j++;
60+
cnt++;
61+
} else if (xiaotiantian.get(i) < xiaoqiqi.get(y)) {//否则,让田忌最差的马和齐王最好的好比一场
62+
i++;
63+
y--;
64+
}
65+
}
66+
System.out.println("田忌可以赢得的比赛场次是:" + cnt);
67+
xiaotiantian.clear();
68+
xiaoqiqi.clear();
69+
}
70+
}
71+
}
72+
```

notes/ali/田忌赛马/TianJi.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import java.util.List;
4+
import java.util.Scanner;
5+
6+
public class TianJi {
7+
public static void main(String[] args) {
8+
List<Integer> xiaotiantian = new ArrayList<Integer>();
9+
List<Integer> xiaoqiqi = new ArrayList<Integer>();
10+
Scanner sc = new Scanner(System.in);
11+
System.out.println("请输入一串数字数组,并用空格分隔(田忌的马):");
12+
String a = sc.nextLine();
13+
String[] arr1 = a.split(" ");
14+
for (String s : arr1) {
15+
xiaotiantian.add(Integer.parseInt(s));
16+
}
17+
System.out.println("请输入一串数字数组,并用空格分隔(齐王的马):");
18+
String b = sc.nextLine();
19+
String[] arr2 = b.split(" ");
20+
for (int i = 0; i < arr1.length; i++) {
21+
xiaoqiqi.add(Integer.parseInt(arr2[i]));
22+
}
23+
int n = xiaotiantian.size();
24+
boolean bLast = true;
25+
while (bLast) {
26+
if (n == 0) {
27+
break;
28+
}
29+
//处理数据
30+
Collections.sort(xiaotiantian);
31+
Collections.sort(xiaoqiqi);
32+
int i = 0, j = 0, x = n - 1, y = n - 1, cnt = 0;
33+
while (bLast) {
34+
//是否是最后一匹马
35+
if (x == i) {
36+
bLast = false;
37+
}
38+
if (xiaotiantian.get(x) > xiaoqiqi.get(y)) {//如果田忌当前最好的马可以胜齐王最好的马,那么比一场
39+
x--;
40+
y--;
41+
cnt++;
42+
} else if (xiaotiantian.get(i) > xiaoqiqi.get(j)) {//如果田忌当前最差的马可以胜齐王最差的马,那么比一场
43+
i++;
44+
j++;
45+
cnt++;
46+
} else if (xiaotiantian.get(i) < xiaoqiqi.get(y)) {//否则,让田忌最差的马和齐王最好的好比一场
47+
i++;
48+
y--;
49+
}
50+
}
51+
System.out.println("田忌可以赢得的比赛场次是:" + cnt);
52+
xiaotiantian.clear();
53+
xiaoqiqi.clear();
54+
}
55+
}
56+
}

notes/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>org.example</groupId>
8+
<artifactId>notes</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
12+
</project>

notes/target/classes/TianJi.class

2.36 KB
Binary file not shown.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#正文:
2+
##要求:
3+
输入两行数字,中间用空格分开,第一行代表田忌的马,第二行代表齐王的马,数字的大小代表马跑的速度,数字越大跑的越快,当两个数字相同时田忌不算赢,要求输出田忌最多能赢多少场
4+
###例 :
5+
```text
6+
输入: 6 4 3 1
7+
8 5 4 2
8+
输出:2
9+
```
10+
###分析:
11+
这里的输入的是字符串,涉及到字符串转int型的数据类型转换,还要求按照其原理进行排兵布阵
12+
###代码:
13+
```java
14+
import java.util.ArrayList;
15+
import java.util.Collections;
16+
import java.util.List;
17+
import java.util.Scanner;
18+
19+
public class TianJi {
20+
public static void main(String[] args) {
21+
List<Integer> xiaotiantian = new ArrayList<Integer>();
22+
List<Integer> xiaoqiqi = new ArrayList<Integer>();
23+
Scanner sc = new Scanner(System.in);
24+
System.out.println("请输入一串数字数组,并用空格分隔(田忌的马):");
25+
String a = sc.nextLine();
26+
String[] arr1 = a.split(" ");
27+
for (String s : arr1) {
28+
xiaotiantian.add(Integer.parseInt(s));
29+
}
30+
System.out.println("请输入一串数字数组,并用空格分隔(齐王的马):");
31+
String b = sc.nextLine();
32+
String[] arr2 = b.split(" ");
33+
for (int i = 0; i < arr1.length; i++) {
34+
xiaoqiqi.add(Integer.parseInt(arr2[i]));
35+
}
36+
int n = xiaotiantian.size();
37+
boolean bLast = true;
38+
while (bLast) {
39+
if (n == 0) {
40+
break;
41+
}
42+
//处理数据
43+
Collections.sort(xiaotiantian);
44+
Collections.sort(xiaoqiqi);
45+
int i = 0, j = 0, x = n - 1, y = n - 1, cnt = 0;
46+
while (bLast) {
47+
//是否是最后一匹马
48+
if (x == i) {
49+
bLast = false;
50+
}
51+
if (xiaotiantian.get(x) > xiaoqiqi.get(y)) {//如果田忌当前最好的马可以胜齐王最好的马,那么比一场
52+
x--;
53+
y--;
54+
cnt++;
55+
} else if (xiaotiantian.get(i) > xiaoqiqi.get(j)) {//如果田忌当前最差的马可以胜齐王最差的马,那么比一场
56+
i++;
57+
j++;
58+
cnt++;
59+
} else if (xiaotiantian.get(i) < xiaoqiqi.get(y)) {//否则,让田忌最差的马和齐王最好的好比一场
60+
i++;
61+
y--;
62+
}
63+
}
64+
System.out.println("田忌可以赢得的比赛场次是:" + cnt);
65+
xiaotiantian.clear();
66+
xiaoqiqi.clear();
67+
}
68+
}
69+
}
70+
```

0 commit comments

Comments
 (0)