Skip to content

Instantly share code, notes, and snippets.

@aoi0308
Created March 16, 2012 02:22
Show Gist options
  • Save aoi0308/2048155 to your computer and use it in GitHub Desktop.
Save aoi0308/2048155 to your computer and use it in GitHub Desktop.
所持金と支払価格から、お釣りが最小になる払い方を求める。
/**
* do execute.
* $ scala payment.scala {pocket} {price}
*/
object Main {
implicit val coins = List(1, 5, 10, 50, 100, 500, 1000, 5000, 10000)
def main(args: Array[String]): Unit = {
if (args.length != 2) sys.exit(-1)
val Array(pocket, price) = args.map(_.toInt)
if (pocket < price) sys.exit(-1)
val payment = getNumberOfCoinsOfPayment(pocket, price)
(coins zip payment).foreach(println)
}
def getNumberOfCoinsOfPayment(pocket: Int, price: Int): List[Int] = (getNumberOfCoins(pocket), getNumberOfCoins(pocket - price)).zipped.map(_-_).map(minusToZero)
def getNumberOfCoins(money: Int)(implicit coins: List[Int]): List[Int] = (coins :\ (money, List[Int]())) {case (c, (b, list)) => (b % c, (b / c) :: list)}._2
def minusToZero(n: Int): Int = if (n > 0) n else 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment