mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-14 04:30:58 +00:00
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:
@@ -0,0 +1,8 @@
|
||||
add_executable(coin_change_greedy coin_change_greedy.c)
|
||||
add_executable(fractional_knapsack fractional_knapsack.c)
|
||||
add_executable(max_capacity max_capacity.c)
|
||||
add_executable(max_product_cutting max_product_cutting.c)
|
||||
|
||||
if (NOT CMAKE_C_COMPILER_ID STREQUAL "MSVC")
|
||||
target_link_libraries(max_product_cutting m)
|
||||
endif()
|
||||
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* File: coin_change_greedy.c
|
||||
* Created Time: 2023-09-07
|
||||
* Author: lwbaptx (lwbaptx@gmail.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* コイン交換:貪欲法 */
|
||||
int coinChangeGreedy(int *coins, int size, int amt) {
|
||||
// coins リストはソート済みと仮定する
|
||||
int i = 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;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main() {
|
||||
// 貪欲法:大域最適解を保証できる
|
||||
int coins1[6] = {1, 5, 10, 20, 50, 100};
|
||||
int amt = 186;
|
||||
int res = coinChangeGreedy(coins1, 6, amt);
|
||||
printf("\ncoins = ");
|
||||
printArray(coins1, 6);
|
||||
printf("amt = %d\n", amt);
|
||||
printf("%d を作るのに必要な最小硬貨枚数は %d です\n", amt, res);
|
||||
|
||||
// 貪欲法:大域最適解を保証できない
|
||||
int coins2[3] = {1, 20, 50};
|
||||
amt = 60;
|
||||
res = coinChangeGreedy(coins2, 3, amt);
|
||||
printf("\ncoins = ");
|
||||
printArray(coins2, 3);
|
||||
printf("amt = %d\n", amt);
|
||||
printf("%d を作るのに必要な最小硬貨枚数は %d です\n", amt, res);
|
||||
printf("実際に必要な最小枚数は 3、つまり 20 + 20 + 20 です\n");
|
||||
|
||||
// 貪欲法:大域最適解を保証できない
|
||||
int coins3[3] = {1, 49, 50};
|
||||
amt = 98;
|
||||
res = coinChangeGreedy(coins3, 3, amt);
|
||||
printf("\ncoins = ");
|
||||
printArray(coins3, 3);
|
||||
printf("amt = %d\n", amt);
|
||||
printf("%d を作るのに必要な最小硬貨枚数は %d です\n", amt, res);
|
||||
printf("実際に必要な最小枚数は 2、つまり 49 + 49 です\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
/**
|
||||
* File: fractional_knapsack.c
|
||||
* Created Time: 2023-09-14
|
||||
* Author: xianii (xianyi.xia@outlook.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 品物 */
|
||||
typedef struct {
|
||||
int w; // 品物の重さ
|
||||
int v; // 品物の価値
|
||||
} Item;
|
||||
|
||||
/* 価値密度でソート */
|
||||
int sortByValueDensity(const void *a, const void *b) {
|
||||
Item *t1 = (Item *)a;
|
||||
Item *t2 = (Item *)b;
|
||||
return (float)(t1->v) / t1->w < (float)(t2->v) / t2->w;
|
||||
}
|
||||
|
||||
/* 分数ナップサック:貪欲法 */
|
||||
float fractionalKnapsack(int wgt[], int val[], int itemCount, int cap) {
|
||||
// 重さと価値の 2 属性を持つ品物リストを作成
|
||||
Item *items = malloc(sizeof(Item) * itemCount);
|
||||
for (int i = 0; i < itemCount; i++) {
|
||||
items[i] = (Item){.w = wgt[i], .v = val[i]};
|
||||
}
|
||||
// 単位価値 item.v / item.w の高い順にソートする
|
||||
qsort(items, (size_t)itemCount, sizeof(Item), sortByValueDensity);
|
||||
// 貪欲選択を繰り返す
|
||||
float res = 0.0;
|
||||
for (int i = 0; i < itemCount; i++) {
|
||||
if (items[i].w <= cap) {
|
||||
// 残り容量が十分なら、現在の品物を丸ごとナップサックに入れる
|
||||
res += items[i].v;
|
||||
cap -= items[i].w;
|
||||
} else {
|
||||
// 残り容量が足りない場合は、現在の品物の一部だけをナップサックに入れる
|
||||
res += (float)cap / items[i].w * items[i].v;
|
||||
cap = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
free(items);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main(void) {
|
||||
int wgt[] = {10, 20, 30, 40, 50};
|
||||
int val[] = {50, 120, 150, 210, 240};
|
||||
int capacity = 50;
|
||||
|
||||
// 貪欲法
|
||||
float res = fractionalKnapsack(wgt, val, sizeof(wgt) / sizeof(int), capacity);
|
||||
printf("ナップサック容量を超えない最大の品物価値は %0.2f です\n", res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* File: max_capacity.c
|
||||
* Created Time: 2023-09-15
|
||||
* Author: xianii (xianyi.xia@outlook.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 最小値を求める */
|
||||
int myMin(int a, int b) {
|
||||
return a < b ? a : b;
|
||||
}
|
||||
/* 最大値を求める */
|
||||
int myMax(int a, int b) {
|
||||
return a > b ? a : b;
|
||||
}
|
||||
|
||||
/* 最大容量:貪欲法 */
|
||||
int maxCapacity(int ht[], int htLength) {
|
||||
// i, j を初期化し、それぞれ配列の両端に置く
|
||||
int i = 0;
|
||||
int j = htLength - 1;
|
||||
// 初期の最大容量は 0
|
||||
int res = 0;
|
||||
// 2 枚の板が出会うまで貪欲選択を繰り返す
|
||||
while (i < j) {
|
||||
// 最大容量を更新する
|
||||
int capacity = myMin(ht[i], ht[j]) * (j - i);
|
||||
res = myMax(res, capacity);
|
||||
// 短い方を内側へ動かす
|
||||
if (ht[i] < ht[j]) {
|
||||
i++;
|
||||
} else {
|
||||
j--;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main(void) {
|
||||
int ht[] = {3, 8, 5, 2, 7, 7, 3, 4};
|
||||
|
||||
// 貪欲法
|
||||
int res = maxCapacity(ht, sizeof(ht) / sizeof(int));
|
||||
printf("最大容量は %d です\n", res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/**
|
||||
* File: max_product_cutting.c
|
||||
* Created Time: 2023-09-15
|
||||
* Author: xianii (xianyi.xia@outlook.com)
|
||||
*/
|
||||
|
||||
#include "../utils/common.h"
|
||||
|
||||
/* 最大切断積:貪欲法 */
|
||||
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 pow(3, a - 1) * 2 * 2;
|
||||
}
|
||||
if (b == 2) {
|
||||
// 余りが 2 のときは、そのままにする
|
||||
return pow(3, a) * 2;
|
||||
}
|
||||
// 余りが 0 のときは、そのままにする
|
||||
return pow(3, a);
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
int main(void) {
|
||||
int n = 58;
|
||||
// 貪欲法
|
||||
int res = maxProductCutting(n);
|
||||
printf("最大分割積は %d です\n", res);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user