forked from algorithmzuo/algorithmbasic2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7f7f0b
commit 009a290
Showing
7 changed files
with
173 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package class22; | ||
|
||
public class Code03_SplitNumber { | ||
|
||
// n为正数 | ||
public static int ways(int n) { | ||
if (n < 0) { | ||
return 0; | ||
} | ||
if (n == 1) { | ||
return 1; | ||
} | ||
return process(1, n); | ||
} | ||
|
||
// 上一个拆出来的数是pre | ||
// 还剩rest需要去拆 | ||
// 返回拆解的方法数 | ||
public static int process(int pre, int rest) { | ||
if (rest == 0) { | ||
return 1; | ||
} | ||
if (pre > rest) { | ||
return 0; | ||
} | ||
int ways = 0; | ||
for (int first = pre; first <= rest; first++) { | ||
ways += process(first, rest - first); | ||
} | ||
return ways; | ||
} | ||
|
||
public static int dp1(int n) { | ||
if (n < 0) { | ||
return 0; | ||
} | ||
if (n == 1) { | ||
return 1; | ||
} | ||
int[][] dp = new int[n + 1][n + 1]; | ||
for (int pre = 1; pre <= n; pre++) { | ||
dp[pre][0] = 1; | ||
dp[pre][pre] = 1; | ||
} | ||
for (int pre = n - 1; pre >= 1; pre--) { | ||
for (int rest = pre + 1; rest <= n; rest++) { | ||
int ways = 0; | ||
for (int first = pre; first <= rest; first++) { | ||
ways += dp[first][rest - first]; | ||
} | ||
dp[pre][rest] = ways; | ||
} | ||
} | ||
return dp[1][n]; | ||
} | ||
|
||
public static int dp2(int n) { | ||
if (n < 0) { | ||
return 0; | ||
} | ||
if (n == 1) { | ||
return 1; | ||
} | ||
int[][] dp = new int[n + 1][n + 1]; | ||
for (int pre = 1; pre <= n; pre++) { | ||
dp[pre][0] = 1; | ||
dp[pre][pre] = 1; | ||
} | ||
for (int pre = n - 1; pre >= 1; pre--) { | ||
for (int rest = pre + 1; rest <= n; rest++) { | ||
dp[pre][rest] = dp[pre + 1][rest]; | ||
dp[pre][rest] += dp[pre][rest - pre]; | ||
} | ||
} | ||
return dp[1][n]; | ||
} | ||
|
||
public static void main(String[] args) { | ||
int test = 39; | ||
System.out.println(ways(test)); | ||
System.out.println(dp1(test)); | ||
System.out.println(dp2(test)); | ||
} | ||
|
||
} |
4 changes: 2 additions & 2 deletions
4
src/class22/Code03_SplitSumClosed.java → src/class23/Code01_SplitSumClosed.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...lass22/Code04_SplitSumClosedSizeHalf.java → ...lass23/Code02_SplitSumClosedSizeHalf.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/class23/Code01_NQueens.java → src/class23/Code03_NQueens.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters