mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 04:31:55 +08:00
docs: add Japanese translate documents (#1812)
* docs: add Japanese documents (`ja/docs`) * docs: add Japanese documents (`ja/codes`) * docs: add Japanese documents * Remove pythontutor blocks in ja/ * Add an empty at the end of each markdown file. * Add the missing figures (use the English version temporarily). * Add index.md for Japanese version. * Add index.html for Japanese version. * Add missing index.assets * Fix backtracking_algorithm.md for Japanese version. * Add avatar_eltociear.jpg. Fix image links on the Japanese landing page. * Add the Japanese banner. --------- Co-authored-by: krahets <krahets@163.com>
This commit is contained in:
committed by
GitHub
parent
2487a27036
commit
954c45864b
60
ja/codes/cpp/chapter_greedy/coin_change_greedy.cpp
Normal file
60
ja/codes/cpp/chapter_greedy/coin_change_greedy.cpp
Normal file
@ -0,0 +1,60 @@
|
||||
/**
|
||||
* File: coin_change_greedy.cpp
|
||||
* Created Time: 2023-07-20
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 硬貨両替:貪欲法 */
|
||||
int coinChangeGreedy(vector<int> &coins, int amt) {
|
||||
// 硬貨リストが順序付けされていると仮定
|
||||
int i = coins.size() - 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;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
// 貪欲法:大域最適解の発見を保証できる
|
||||
vector<int> coins = {1, 5, 10, 20, 50, 100};
|
||||
int amt = 186;
|
||||
int res = coinChangeGreedy(coins, amt);
|
||||
cout << "\ncoins = ";
|
||||
printVector(coins);
|
||||
cout << "amt = " << amt << endl;
|
||||
cout << amt << " を作るのに必要な最小硬貨数は " << res << " です" << endl;
|
||||
|
||||
// 貪欲法:大域最適解の発見を保証できない
|
||||
coins = {1, 20, 50};
|
||||
amt = 60;
|
||||
res = coinChangeGreedy(coins, amt);
|
||||
cout << "\ncoins = ";
|
||||
printVector(coins);
|
||||
cout << "amt = " << amt << endl;
|
||||
cout << amt << " を作るのに必要な最小硬貨数は " << res << " です" << endl;
|
||||
cout << "実際には、最小必要数は 3 です。つまり、20 + 20 + 20" << endl;
|
||||
|
||||
// 貪欲法:大域最適解の発見を保証できない
|
||||
coins = {1, 49, 50};
|
||||
amt = 98;
|
||||
res = coinChangeGreedy(coins, amt);
|
||||
cout << "\ncoins = ";
|
||||
printVector(coins);
|
||||
cout << "amt = " << amt << endl;
|
||||
cout << amt << " を作るのに必要な最小硬貨数は " << res << " です" << endl;
|
||||
cout << "実際には、最小必要数は 2 です。つまり、49 + 49" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
56
ja/codes/cpp/chapter_greedy/fractional_knapsack.cpp
Normal file
56
ja/codes/cpp/chapter_greedy/fractional_knapsack.cpp
Normal file
@ -0,0 +1,56 @@
|
||||
/**
|
||||
* File: fractional_knapsack.cpp
|
||||
* Created Time: 2023-07-20
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* アイテム */
|
||||
class Item {
|
||||
public:
|
||||
int w; // アイテムの重量
|
||||
int v; // アイテムの価値
|
||||
|
||||
Item(int w, int v) : w(w), v(v) {
|
||||
}
|
||||
};
|
||||
|
||||
/* 分数ナップサック:貪欲法 */
|
||||
double fractionalKnapsack(vector<int> &wgt, vector<int> &val, int cap) {
|
||||
// アイテムリストを作成、2つの属性を含む:重量、価値
|
||||
vector<Item> items;
|
||||
for (int i = 0; i < wgt.size(); i++) {
|
||||
items.push_back(Item(wgt[i], val[i]));
|
||||
}
|
||||
// 単位価値 item.v / item.w で高い順にソート
|
||||
sort(items.begin(), items.end(), [](Item &a, Item &b) { return (double)a.v / a.w > (double)b.v / b.w; });
|
||||
// 貪欲選択をループ
|
||||
double res = 0;
|
||||
for (auto &item : items) {
|
||||
if (item.w <= cap) {
|
||||
// 残り容量が十分な場合、アイテム全体をナップサックに入れる
|
||||
res += item.v;
|
||||
cap -= item.w;
|
||||
} else {
|
||||
// 残り容量が不十分な場合、アイテムの一部をナップサックに入れる
|
||||
res += (double)item.v / item.w * cap;
|
||||
// 残り容量がなくなったため、ループを中断
|
||||
break;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
vector<int> wgt = {10, 20, 30, 40, 50};
|
||||
vector<int> val = {50, 120, 150, 210, 240};
|
||||
int cap = 50;
|
||||
|
||||
// 貪欲アルゴリズム
|
||||
double res = fractionalKnapsack(wgt, val, cap);
|
||||
cout << "ナップサック容量内での最大値は " << res << " です" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
39
ja/codes/cpp/chapter_greedy/max_capacity.cpp
Normal file
39
ja/codes/cpp/chapter_greedy/max_capacity.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* File: max_capacity.cpp
|
||||
* Created Time: 2023-07-21
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 最大容量:貪欲法 */
|
||||
int maxCapacity(vector<int> &ht) {
|
||||
// i、j を初期化し、配列の両端で分割させる
|
||||
int i = 0, j = ht.size() - 1;
|
||||
// 初期最大容量は 0
|
||||
int res = 0;
|
||||
// 2つの板が出会うまで貪欲選択をループ
|
||||
while (i < j) {
|
||||
// 最大容量を更新
|
||||
int cap = min(ht[i], ht[j]) * (j - i);
|
||||
res = max(res, cap);
|
||||
// より短い板を内側に移動
|
||||
if (ht[i] < ht[j]) {
|
||||
i++;
|
||||
} else {
|
||||
j--;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
vector<int> ht = {3, 8, 5, 2, 7, 7, 3, 4};
|
||||
|
||||
// 貪欲アルゴリズム
|
||||
int res = maxCapacity(ht);
|
||||
cout << "最大容量は " << res << " です" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
39
ja/codes/cpp/chapter_greedy/max_product_cutting.cpp
Normal file
39
ja/codes/cpp/chapter_greedy/max_product_cutting.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
/**
|
||||
* File: max_product_cutting.cpp
|
||||
* Created Time: 2023-07-21
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.hpp"
|
||||
|
||||
/* 最大積切断:貪欲法 */
|
||||
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)pow(3, a - 1) * 2 * 2;
|
||||
}
|
||||
if (b == 2) {
|
||||
// 余りが 2 の場合、何もしない
|
||||
return (int)pow(3, a) * 2;
|
||||
}
|
||||
// 余りが 0 の場合、何もしない
|
||||
return (int)pow(3, a);
|
||||
}
|
||||
|
||||
/* ドライバーコード */
|
||||
int main() {
|
||||
int n = 58;
|
||||
|
||||
// 貪欲アルゴリズム
|
||||
int res = maxProductCutting(n);
|
||||
cout << "分割の最大積は " << res << " です" << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user