mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-01 11:29:51 +08:00
* 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>
60 lines
2.0 KiB
C++
60 lines
2.0 KiB
C++
/**
|
|
* 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;
|
|
} |