mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-11-01 03:24:24 +08:00 
			
		
		
		
	 954c45864b
			
		
	
	954c45864b
	
	
	
		
			
			* 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>
		
			
				
	
	
		
			55 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
			
		
		
	
	
			55 lines
		
	
	
		
			2.2 KiB
		
	
	
	
		
			Java
		
	
	
	
	
	
| /**
 | |
|  * 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");
 | |
|     }
 | |
| } |