scala递归实现换零钱算法

 import scala.collection.mutable.ArrayBuffer
import scala.util.control.Breaks object Exchange {
def delete(arg:ArrayBuffer[Int]):ArrayBuffer[Int]={
val breakables = new Breaks
breakables.breakable {
for (i <- 0 until arg.length) {
if (arg(i) == arg.max) {
arg.remove(i)
breakables.break()
}
}
}
arg
}
def exchange(total:Int,money:ArrayBuffer[Int]):Int={
if (total==0)
1
else if(total<0||money.length==0)
0
else{
//var newMoney=money这种赋值方式,newMoney其实不是一个新的数组
val newMoney = for(elem <- money) yield elem
delete(newMoney)
exchange((total-money.max),money)+exchange(total,newMoney)
}
}
def main(args:Array[String]): Unit ={
val ab = ArrayBuffer[Int](50,20,10,5,2,1)
println(exchange(100,ab))
}
}
上一篇:PyCharm+Scrapy爬取安居客楼盘信息


下一篇:2018.09.15 hdu1599find the mincost route(floyd求最小环)