1 import scala.collection.mutable.ArrayBuffer 2 import scala.util.control.Breaks 3 4 object Exchange { 5 def delete(arg:ArrayBuffer[Int]):ArrayBuffer[Int]={ 6 val breakables = new Breaks 7 breakables.breakable { 8 for (i <- 0 until arg.length) { 9 if (arg(i) == arg.max) { 10 arg.remove(i) 11 breakables.break() 12 } 13 } 14 } 15 arg 16 } 17 def exchange(total:Int,money:ArrayBuffer[Int]):Int={ 18 if (total==0) 19 1 20 else if(total<0||money.length==0) 21 0 22 else{ 23 //var newMoney=money这种赋值方式,newMoney其实不是一个新的数组 24 val newMoney = for(elem <- money) yield elem 25 delete(newMoney) 26 exchange((total-money.max),money)+exchange(total,newMoney) 27 } 28 } 29 def main(args:Array[String]): Unit ={ 30 val ab = ArrayBuffer[Int](50,20,10,5,2,1) 31 println(exchange(100,ab)) 32 } 33 }