Created
September 8, 2014 16:23
-
-
Save ug23/c2c9e9f6461ecac6f2fe to your computer and use it in GitHub Desktop.
CTO challenge Level1
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
/** | |
* 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