mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-04 04:02:48 +08:00
Retranslate the complete Japanese online edition from Chinese (#2008)
* Retranslate the complete Japanese online edition from Chinese * Fit Japanese homepage labels and localize heading permalinks * Point Japanese contribution instructions to the Japanese preview * Replace locale conditionals with multilingual UI tables
This commit is contained in:
@@ -7,19 +7,19 @@
|
||||
namespace hello_algo.chapter_dynamic_programming;
|
||||
|
||||
public class unbounded_knapsack {
|
||||
/* 完全ナップサック問題:動的計画法 */
|
||||
/* 個数制限なしナップサック問題:動的計画法 */
|
||||
int UnboundedKnapsackDP(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.Length;
|
||||
// dp テーブルを初期化
|
||||
// dp テーブルを初期化する
|
||||
int[,] dp = new int[n + 1, cap + 1];
|
||||
// 状態遷移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int c = 1; c <= cap; c++) {
|
||||
if (wgt[i - 1] > c) {
|
||||
// ナップサック容量を超えるなら品物 i は選ばない
|
||||
// ナップサックの容量を超える場合、品物 i を選ばない
|
||||
dp[i, c] = dp[i - 1, c];
|
||||
} else {
|
||||
// 品物 i を選ばない場合と選ぶ場合の大きい方
|
||||
// 品物 i を選ばない場合と選ぶ場合の大きいほうの値
|
||||
dp[i, c] = Math.Max(dp[i - 1, c], dp[i, c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
@@ -27,19 +27,19 @@ public class unbounded_knapsack {
|
||||
return dp[n, cap];
|
||||
}
|
||||
|
||||
/* 完全ナップサック問題:空間最適化後の動的計画法 */
|
||||
/* 個数制限なしナップサック問題:空間を最適化した動的計画法 */
|
||||
int UnboundedKnapsackDPComp(int[] wgt, int[] val, int cap) {
|
||||
int n = wgt.Length;
|
||||
// dp テーブルを初期化
|
||||
// dp テーブルを初期化する
|
||||
int[] dp = new int[cap + 1];
|
||||
// 状態遷移
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int c = 1; c <= cap; c++) {
|
||||
if (wgt[i - 1] > c) {
|
||||
// ナップサック容量を超えるなら品物 i は選ばない
|
||||
// ナップサックの容量を超える場合、品物 i を選ばない
|
||||
dp[c] = dp[c];
|
||||
} else {
|
||||
// 品物 i を選ばない場合と選ぶ場合の大きい方
|
||||
// 品物 i を選ばない場合と選ぶ場合の大きいほうの値
|
||||
dp[c] = Math.Max(dp[c], dp[c - wgt[i - 1]] + val[i - 1]);
|
||||
}
|
||||
}
|
||||
@@ -55,10 +55,10 @@ public class unbounded_knapsack {
|
||||
|
||||
// 動的計画法
|
||||
int res = UnboundedKnapsackDP(wgt, val, cap);
|
||||
Console.WriteLine("バックパック容量を超えない最大価値は " + res);
|
||||
Console.WriteLine("ナップサックの容量を超えない品物の最大価値は " + res);
|
||||
|
||||
// 空間最適化後の動的計画法
|
||||
// 空間を最適化した動的計画法
|
||||
res = UnboundedKnapsackDPComp(wgt, val, cap);
|
||||
Console.WriteLine("バックパック容量を超えない最大価値は " + res);
|
||||
Console.WriteLine("ナップサックの容量を超えない品物の最大価値は " + res);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user