#!/bin/bash # = お釣りの枚数が最少となる支払い方法を教えてくれるスクリプト # * 作成者: 藤林 凜 # * 履歴: # * メモ: 変数 a から i までの組み合わせが多い場合, 実行に時間がかかる echo "1 円玉の枚数を入力してください:" read a echo "5 円玉の枚数を入力してください:" read b echo "10 円玉の枚数を入力してください:" read c echo "50 円玉の枚数を入力してください:" read d echo "100 円玉の枚数を入力してください:" read e echo "500 円玉の枚数を入力してください:" read f echo "1000 円札の枚数を入力してください:" read g echo "5000 円札の枚数を入力してください:" read h echo "10000 円札の枚数を入力してください:" read i echo "購入金額を入力してください:" read x echo "待っててね..." max=` expr 1 "*" $a + 5 "*" $b + 10 "*" $c + 50 "*" $d + 100 "*" $e + 500 "*" $f + 1000 "*" $g + 5000 "*" $h + 10000 "*" $i ` # 手持ち金額に不足がないか確認する if [ $x -gt $max ] then echo "手持ちのお金が不足しています. 商品を棚に戻してお家に帰りましょう." exit fi # 手持ちのお金の組み合わせを全て列挙する declare -a array for j in ` seq 0 $a ` do for k in ` seq 0 $b ` do for l in ` seq 0 $c ` do for m in ` seq 0 $d ` do for n in ` seq 0 $e ` do for o in ` seq 0 $f ` do for p in ` seq 0 $g ` do for q in ` seq 0 $h ` do for r in ` seq 0 $i ` do y=` expr 1 "*" $j + 5 "*" $k + 10 "*" $l + 50 "*" $m + 100 "*" $n + 500 "*" $o + 1000 "*" $p + 5000 "*" $q + 10000 "*" $r ` if [ $y -ge $x ] then dif=` expr $y - $x ` cha_10000=` expr $dif / 10000 ` cha_10000_sur=` expr $dif % 10000 ` cha_5000=` expr $cha_10000_sur / 5000 ` cha_5000_sur=` expr $cha_10000_sur % 5000 ` cha_1000=` expr $cha_5000_sur / 1000 ` cha_1000_sur=` expr $cha_5000_sur % 1000 ` cha_500=` expr $cha_1000_sur / 500 ` cha_500_sur=` expr $cha_1000_sur % 500 ` cha_100=` expr $cha_500_sur / 100 ` cha_100_sur=` expr $cha_500_sur % 100 ` cha_50=` expr $cha_100_sur / 50 ` cha_50_sur=` expr $cha_100_sur % 50 ` cha_10=` expr $cha_50_sur / 10 ` cha_10_sur=` expr $cha_50_sur % 10 ` cha_5=` expr $cha_10_sur / 5 ` cha_5_sur=` expr $cha_10_sur % 5 ` cha_1=` expr $cha_5_sur / 1 ` cha_sum=` expr $cha_1 + $cha_5 + $cha_10 + $cha_50 + $cha_100 + $cha_500 + $cha_1000 + $cha_5000 + $cha_10000 ` array+=( "$j,$k,$l,$m,$n,$o,$p,$q,$r,$cha_sum" ) fi done done done done done done done done done # 上で列挙した組み合わせを予備ファイルに書き込む z=` echo ${#array[*]} ` l=0 while [ $l -le $z ] do echo ${array["$l"]} >> yobi.txt l=` expr $l + 1 ` done min=1000000 cut -d , -f 10 yobi.txt > yobi2.txt for s in ` cut -d , -f 10 yobi.txt ` do if [ $s -lt $min ] then min=$s fi done grep -e "$min" -n yobi2.txt | sed -e 's/:.*//g' > yobi3.txt v=` cat yobi3.txt | wc -l ` loop=1 for u in ` grep -e "$min" -n yobi2.txt | sed -e 's/:.*//g' ` do sed -n "$u"p yobi.txt > hoge$u.txt if [[ ` cut -d , -f 1 hoge$u.txt ` -ne 0 ]] then echo "1 円玉を "` cut -d , -f 1 hoge$u.txt `" 枚" fi if [[ ` cut -d , -f 2 hoge$u.txt ` -ne 0 ]] then echo "5 円玉を "` cut -d , -f 2 hoge$u.txt `" 枚" fi if [[ ` cut -d , -f 3 hoge$u.txt ` -ne 0 ]] then echo "10 円玉を "` cut -d , -f 3 hoge$u.txt `" 枚" fi if [[ ` cut -d , -f 4 hoge$u.txt ` -ne 0 ]] then echo "50 円玉を "` cut -d , -f 4 hoge$u.txt `" 枚" fi if [[ ` cut -d , -f 5 hoge$u.txt ` -ne 0 ]] then echo "100 円玉を "` cut -d , -f 5 hoge$u.txt `" 枚" fi if [[ ` cut -d , -f 6 hoge$u.txt ` -ne 0 ]] then echo "500 円玉を "` cut -d , -f 6 hoge$u.txt `" 枚" fi if [[ ` cut -d , -f 7 hoge$u.txt ` -ne 0 ]] then echo "1000 円札を "` cut -d , -f 7 hoge$u.txt `" 枚" fi if [[ ` cut -d , -f 8 hoge$u.txt ` -ne 0 ]] then echo "5000 円札を "` cut -d , -f 8 hoge$u.txt `" 枚" fi if [[ ` cut -d , -f 9 hoge$u.txt ` -ne 0 ]] then echo "10000 円札を "` cut -d , -f 9 hoge$u.txt `" 枚" fi if [ $v -ne 1 -a $loop -ne $v ] then echo "もしくは" fi loop=` expr $loop + 1 ` done echo "出してください" rm yobi*txt hoge*txt