JOI雑貨店問題を解いてみた
ちょっと気になったので
この問題↓ を解いてみた
太郎君はよくJOI雑貨店で買い物をする。JOI雑貨店には500円、100円、50円、10円、5円、1円が十分な数だけあり、いつも最も枚数が少なくなるようなおつりの支払い方をする。レジで1000円札を1枚出した時、硬貨の枚数を求めるプログラムを作成せよ。
http://matome.naver.jp/odai/2137768077518337401
public static void main(String[] args) { System.out.println(getCoinCount(620)); } public static final int[] COINS = {500, 100, 50, 10, 5, 1}; //お釣りを入力すると硬貨の数をカウントする public static int getCoinCount(int yen){ for(int coin : COINS){ if( yen >= coin ){ System.out.println(yen + " [" + coin + "]"); return getCoinCount(yen-coin) + 1; } } return 0; }
これ、どっかで見た問題だな
SICPだったけな。。。