mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-14 02:10:37 +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
55
ja/codes/java/chapter_greedy/coin_change_greedy.java
Normal file
55
ja/codes/java/chapter_greedy/coin_change_greedy.java
Normal file
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* File: coin_change_greedy.java
|
||||
* Created Time: 2023-07-20
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_greedy;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class coin_change_greedy {
|
||||
/* 硬貨両替:貪欲法 */
|
||||
static int coinChangeGreedy(int[] coins, int amt) {
|
||||
// 硬貨リストが順序付けされていると仮定
|
||||
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;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
// 貪欲法:大域最適解の発見を保証できる
|
||||
int[] coins = { 1, 5, 10, 20, 50, 100 };
|
||||
int amt = 186;
|
||||
int res = coinChangeGreedy(coins, amt);
|
||||
System.out.println("\ncoins = " + Arrays.toString(coins) + ", amt = " + amt);
|
||||
System.out.println(amt + " を作るのに必要な最小硬貨数は " + res + " です");
|
||||
|
||||
// 貪欲法:大域最適解の発見を保証できない
|
||||
coins = new int[] { 1, 20, 50 };
|
||||
amt = 60;
|
||||
res = coinChangeGreedy(coins, amt);
|
||||
System.out.println("\ncoins = " + Arrays.toString(coins) + ", amt = " + amt);
|
||||
System.out.println(amt + " を作るのに必要な最小硬貨数は " + res + " です");
|
||||
System.out.println("実際には、最小必要数は 3 です。つまり、20 + 20 + 20");
|
||||
|
||||
// 貪欲法:大域最適解の発見を保証できない
|
||||
coins = new int[] { 1, 49, 50 };
|
||||
amt = 98;
|
||||
res = coinChangeGreedy(coins, amt);
|
||||
System.out.println("\ncoins = " + Arrays.toString(coins) + ", amt = " + amt);
|
||||
System.out.println(amt + " を作るのに必要な最小硬貨数は " + res + " です");
|
||||
System.out.println("実際には、最小必要数は 2 です。つまり、49 + 49");
|
||||
}
|
||||
}
|
||||
59
ja/codes/java/chapter_greedy/fractional_knapsack.java
Normal file
59
ja/codes/java/chapter_greedy/fractional_knapsack.java
Normal file
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* File: fractional_knapsack.java
|
||||
* Created Time: 2023-07-20
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_greedy;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
|
||||
/* アイテム */
|
||||
class Item {
|
||||
int w; // アイテムの重量
|
||||
int v; // アイテムの価値
|
||||
|
||||
public Item(int w, int v) {
|
||||
this.w = w;
|
||||
this.v = v;
|
||||
}
|
||||
}
|
||||
|
||||
public class fractional_knapsack {
|
||||
/* 分数ナップサック:貪欲法 */
|
||||
static 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 で高い順にソート
|
||||
Arrays.sort(items, Comparator.comparingDouble(item -> -((double) item.v / item.w)));
|
||||
// 貪欲選択をループ
|
||||
double res = 0;
|
||||
for (Item item : items) {
|
||||
if (item.w <= cap) {
|
||||
// 残り容量が十分な場合、アイテム全体をナップサックに入れる
|
||||
res += item.v;
|
||||
cap -= item.w;
|
||||
} else {
|
||||
// 残り容量が不十分な場合、アイテムの一部をナップサックに入れる
|
||||
res += (double) item.v / item.w * cap;
|
||||
// 残り容量がなくなったため、ループを中断
|
||||
break;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] wgt = { 10, 20, 30, 40, 50 };
|
||||
int[] val = { 50, 120, 150, 210, 240 };
|
||||
int cap = 50;
|
||||
|
||||
// 貪欲アルゴリズム
|
||||
double res = fractionalKnapsack(wgt, val, cap);
|
||||
System.out.println("ナップサック容量内での最大値は " + res + " です");
|
||||
}
|
||||
}
|
||||
38
ja/codes/java/chapter_greedy/max_capacity.java
Normal file
38
ja/codes/java/chapter_greedy/max_capacity.java
Normal file
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* File: max_capacity.java
|
||||
* Created Time: 2023-07-21
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_greedy;
|
||||
|
||||
public class max_capacity {
|
||||
/* 最大容量:貪欲法 */
|
||||
static 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;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] ht = { 3, 8, 5, 2, 7, 7, 3, 4 };
|
||||
|
||||
// 貪欲アルゴリズム
|
||||
int res = maxCapacity(ht);
|
||||
System.out.println("最大容量は " + res + " です");
|
||||
}
|
||||
}
|
||||
40
ja/codes/java/chapter_greedy/max_product_cutting.java
Normal file
40
ja/codes/java/chapter_greedy/max_product_cutting.java
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* File: max_product_cutting.java
|
||||
* Created Time: 2023-07-21
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_greedy;
|
||||
|
||||
import java.lang.Math;
|
||||
|
||||
public class max_product_cutting {
|
||||
/* 最大積切断:貪欲法 */
|
||||
public static 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);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int n = 58;
|
||||
|
||||
// 貪欲アルゴリズム
|
||||
int res = maxProductCutting(n);
|
||||
System.out.println("分割の最大積は " + res + " です");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user