Skip to content

Instantly share code, notes, and snippets.

@ug23
Created September 8, 2014 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ug23/c2c9e9f6461ecac6f2fe to your computer and use it in GitHub Desktop.
Save ug23/c2c9e9f6461ecac6f2fe to your computer and use it in GitHub Desktop.
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