Re-translate the Japanese version (#1871)

* Retranslate Japanese docs with GPT-5.4

* Retranslate Japanese code with GPT-5.4
This commit is contained in:
Yudong Jin
2026-03-30 07:30:15 +08:00
committed by GitHub
parent fe6443235b
commit d7b2277d2b
1444 changed files with 83312 additions and 8363 deletions
@@ -0,0 +1,54 @@
/**
* File: coin_change_greedy.cs
* Created Time: 2023-07-21
* Author: hpstory (hpstory1024@163.com)
*/
namespace hello_algo.chapter_greedy;
public class coin_change_greedy {
/* コイン交換:貪欲法 */
int CoinChangeGreedy(int[] coins, int amt) {
// coins リストはソート済みと仮定する
int i = coins.Length - 1;
int count = 0;
// 残額がなくなるまで貪欲選択を繰り返す
while (amt > 0) {
// 残額以下で最も近い硬貨を見つける
while (i > 0 && coins[i] > amt) {
i--;
}
// coins[i] を選択する
amt -= coins[i];
count++;
}
// 実行可能な解が見つからなければ -1 を返す
return amt == 0 ? count : -1;
}
[Test]
public void Test() {
// 貪欲法:大域最適解を保証できる
int[] coins = [1, 5, 10, 20, 50, 100];
int amt = 186;
int res = CoinChangeGreedy(coins, amt);
Console.WriteLine("\ncoins = " + coins.PrintList() + ", amt = " + amt);
Console.WriteLine("金額 " + amt + " を作るのに必要な最小硬貨枚数は " + res);
// 貪欲法:大域最適解を保証できない
coins = [1, 20, 50];
amt = 60;
res = CoinChangeGreedy(coins, amt);
Console.WriteLine("\ncoins = " + coins.PrintList() + ", amt = " + amt);
Console.WriteLine("金額 " + amt + " を作るのに必要な最小硬貨枚数は " + res);
Console.WriteLine("実際に必要な最小枚数は 3、つまり 20 + 20 + 20");
// 貪欲法:大域最適解を保証できない
coins = [1, 49, 50];
amt = 98;
res = CoinChangeGreedy(coins, amt);
Console.WriteLine("\ncoins = " + coins.PrintList() + ", amt = " + amt);
Console.WriteLine("金額 " + amt + " を作るのに必要な最小硬貨枚数は " + res);
Console.WriteLine("実際に必要な最小枚数は 2、つまり 49 + 49");
}
}
@@ -0,0 +1,52 @@
/**
* File: fractional_knapsack.cs
* Created Time: 2023-07-21
* Author: hpstory (hpstory1024@163.com)
*/
namespace hello_algo.chapter_greedy;
/* 品物 */
class Item(int w, int v) {
public int w = w; // 品物の重さ
public int v = v; // 品物の価値
}
public class fractional_knapsack {
/* 分数ナップサック:貪欲法 */
double FractionalKnapsack(int[] wgt, int[] val, int cap) {
// 重さと価値の 2 属性を持つ品物リストを作成
Item[] items = new Item[wgt.Length];
for (int i = 0; i < wgt.Length; i++) {
items[i] = new Item(wgt[i], val[i]);
}
// 単位価値 item.v / item.w の高い順にソートする
Array.Sort(items, (x, y) => (y.v / y.w).CompareTo(x.v / x.w));
// 貪欲選択を繰り返す
double res = 0;
foreach (Item item in items) {
if (item.w <= cap) {
// 残り容量が十分なら、現在の品物を丸ごとナップサックに入れる
res += item.v;
cap -= item.w;
} else {
// 残り容量が足りない場合は、現在の品物の一部だけをナップサックに入れる
res += (double)item.v / item.w * cap;
// 残り容量がないため、ループを抜ける
break;
}
}
return res;
}
[Test]
public void Test() {
int[] wgt = [10, 20, 30, 40, 50];
int[] val = [50, 120, 150, 210, 240];
int cap = 50;
// 貪欲法
double res = FractionalKnapsack(wgt, val, cap);
Console.WriteLine("バックパック容量を超えない最大価値は " + res);
}
}
@@ -0,0 +1,39 @@
/**
* File: max_capacity.cs
* Created Time: 2023-07-21
* Author: hpstory (hpstory1024@163.com)
*/
namespace hello_algo.chapter_greedy;
public class max_capacity {
/* 最大容量:貪欲法 */
int MaxCapacity(int[] ht) {
// i, j を初期化し、それぞれ配列の両端に置く
int i = 0, j = ht.Length - 1;
// 初期の最大容量は 0
int res = 0;
// 2 枚の板が出会うまで貪欲選択を繰り返す
while (i < j) {
// 最大容量を更新する
int cap = Math.Min(ht[i], ht[j]) * (j - i);
res = Math.Max(res, cap);
// 短い方を内側へ動かす
if (ht[i] < ht[j]) {
i++;
} else {
j--;
}
}
return res;
}
[Test]
public void Test() {
int[] ht = [3, 8, 5, 2, 7, 7, 3, 4];
// 貪欲法
int res = MaxCapacity(ht);
Console.WriteLine("最大容量は " + res);
}
}
@@ -0,0 +1,39 @@
/**
* File: max_product_cutting.cs
* Created Time: 2023-07-21
* Author: hpstory (hpstory1024@163.com)
*/
namespace hello_algo.chapter_greedy;
public class max_product_cutting {
/* 最大切断積:貪欲法 */
int MaxProductCutting(int n) {
// n <= 3 のときは、必ず 1 を切り出す
if (n <= 3) {
return 1 * (n - 1);
}
// 貪欲に 3 を切り出し、a を 3 の個数、b を余りとする
int a = n / 3;
int b = n % 3;
if (b == 1) {
// 余りが 1 のときは、1 * 3 を 2 * 2 に変える
return (int)Math.Pow(3, a - 1) * 2 * 2;
}
if (b == 2) {
// 余りが 2 のときは、そのままにする
return (int)Math.Pow(3, a) * 2;
}
// 余りが 0 のときは、そのままにする
return (int)Math.Pow(3, a);
}
[Test]
public void Test() {
int n = 58;
// 貪欲法
int res = MaxProductCutting(n);
Console.WriteLine("最大分割積は" + res);
}
}