Created
March 16, 2012 02:22
-
-
Save aoi0308/2048155 to your computer and use it in GitHub Desktop.
所持金と支払価格から、お釣りが最小になる払い方を求める。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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