Skip to content

Instantly share code, notes, and snippets.

@ug23
Created Sep 8, 2014
Embed
What would you like to do?
CTO challenge Level1
/**
* coupon.scala
*
* Usage:% scala couponopt.scala <支払総額> <50円の枚数> <100円の枚数> <500円の枚数>
* 使用可能でかつ最小の枚数になる組み合わせを表示する
*/
object Coupon{
def selectOptimalCoupons(total:Int, coupon:List[Int]) :Unit= {
val valueList :List[Int] = List(50, 100, 500)
def available(remain :Int, index :Int) :Unit= {
val value = valueList(index)
val amount = {
if((remain / value) < coupon(index))
(remain / value)
else coupon(index)
}
if(index>0){
available( remain - amount * value , index - 1)
}
println( value + "円 : " + amount + "")
}
available(total, 2)
}
def main(args: Array[String]): Unit = {
args.length match {
case 4 =>
val argsToInt = args.map(_.toInt)
selectOptimalCoupons(argsToInt.head, argsToInt.tail.toList)
case _ => println(
"Usage:\"scala coupon.scala <¥total> <# of ¥500> <# of ¥100> <# of ¥50>\"")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment