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.rs
* Created Time: 2023-07-22
* Author: night-cruise (2586447362@qq.com)
*/
/* コイン交換:貪欲法 */
fn coin_change_greedy(coins: &[i32], mut amt: i32) -> i32 {
// coins リストはソート済みと仮定する
let mut i = coins.len() - 1;
let mut count = 0;
// 残額がなくなるまで貪欲選択を繰り返す
while amt > 0 {
// 残額以下で最も近い硬貨を見つける
while i > 0 && coins[i] > amt {
i -= 1;
}
// coins[i] を選択する
amt -= coins[i];
count += 1;
}
// 実行可能な解が見つからなければ -1 を返す
if amt == 0 {
count
} else {
-1
}
}
/* Driver Code */
fn main() {
// 貪欲法:大域最適解を保証できる
let coins = [1, 5, 10, 20, 50, 100];
let amt = 186;
let res = coin_change_greedy(&coins, amt);
println!("\ncoins = {:?}, amt = {}", coins, amt);
println!("{} にするために必要な最小硬貨枚数は {}", amt, res);
// 貪欲法:大域最適解を保証できない
let coins = [1, 20, 50];
let amt = 60;
let res = coin_change_greedy(&coins, amt);
println!("\ncoins = {:?}, amt = {}", coins, amt);
println!("{} にするために必要な最小硬貨枚数は {}", amt, res);
println!("実際に必要な最小枚数は 3、つまり 20 + 20 + 20");
// 貪欲法:大域最適解を保証できない
let coins = [1, 49, 50];
let amt = 98;
let res = coin_change_greedy(&coins, amt);
println!("\ncoins = {:?}, amt = {}", coins, amt);
println!("{} にするために必要な最小硬貨枚数は {}", amt, res);
println!("実際に必要な最小枚数は 2、つまり 49 + 49");
}
@@ -0,0 +1,59 @@
/*
* File: coin_change_greedy.rs
* Created Time: 2023-07-22
* Author: night-cruise (2586447362@qq.com)
*/
/* 品物 */
struct Item {
w: i32, // 品物の重さ
v: i32, // 品物の価値
}
impl Item {
fn new(w: i32, v: i32) -> Self {
Self { w, v }
}
}
/* 分数ナップサック:貪欲法 */
fn fractional_knapsack(wgt: &[i32], val: &[i32], mut cap: i32) -> f64 {
// 重さと価値の 2 属性を持つ品物リストを作成
let mut items = wgt
.iter()
.zip(val.iter())
.map(|(&w, &v)| Item::new(w, v))
.collect::<Vec<Item>>();
// 単位価値 item.v / item.w の高い順にソートする
items.sort_by(|a, b| {
(b.v as f64 / b.w as f64)
.partial_cmp(&(a.v as f64 / a.w as f64))
.unwrap()
});
// 貪欲選択を繰り返す
let mut res = 0.0;
for item in &items {
if item.w <= cap {
// 残り容量が十分なら、現在の品物を丸ごとナップサックに入れる
res += item.v as f64;
cap -= item.w;
} else {
// 残り容量が足りない場合は、現在の品物の一部だけをナップサックに入れる
res += item.v as f64 / item.w as f64 * cap as f64;
// 残り容量がないため、ループを抜ける
break;
}
}
res
}
/* Driver Code */
fn main() {
let wgt = [10, 20, 30, 40, 50];
let val = [50, 120, 150, 210, 240];
let cap = 50;
// 貪欲法
let res = fractional_knapsack(&wgt, &val, cap);
println!("ナップサック容量を超えない最大価値は {}", res);
}
@@ -0,0 +1,36 @@
/*
* File: coin_change_greedy.rs
* Created Time: 2023-07-22
* Author: night-cruise (2586447362@qq.com)
*/
/* 最大容量:貪欲法 */
fn max_capacity(ht: &[i32]) -> i32 {
// i, j を初期化し、それぞれ配列の両端に置く
let mut i = 0;
let mut j = ht.len() - 1;
// 初期の最大容量は 0
let mut res = 0;
// 2 枚の板が出会うまで貪欲選択を繰り返す
while i < j {
// 最大容量を更新する
let cap = std::cmp::min(ht[i], ht[j]) * (j - i) as i32;
res = std::cmp::max(res, cap);
// 短い方を内側へ動かす
if ht[i] < ht[j] {
i += 1;
} else {
j -= 1;
}
}
res
}
/* Driver Code */
fn main() {
let ht = [3, 8, 5, 2, 7, 7, 3, 4];
// 貪欲法
let res = max_capacity(&ht);
println!("最大容量は {}", res);
}
@@ -0,0 +1,35 @@
/*
* File: coin_change_greedy.rs
* Created Time: 2023-07-22
* Author: night-cruise (2586447362@qq.com)
*/
/* 最大切断積:貪欲法 */
fn max_product_cutting(n: i32) -> i32 {
// n <= 3 のときは、必ず 1 を切り出す
if n <= 3 {
return 1 * (n - 1);
}
// 貪欲に 3 を切り出し、a を 3 の個数、b を余りとする
let a = n / 3;
let b = n % 3;
if b == 1 {
// 余りが 1 のときは、1 * 3 を 2 * 2 に変える
3_i32.pow(a as u32 - 1) * 2 * 2
} else if b == 2 {
// 余りが 2 のときは、そのままにする
3_i32.pow(a as u32) * 2
} else {
// 余りが 0 のときは、そのままにする
3_i32.pow(a as u32)
}
}
/* Driver Code */
fn main() {
let n = 58;
// 貪欲法
let res = max_product_cutting(n);
println!("最大分割積は {}", res);
}