zealot0630
2015-11-10 17:36:19 +08:00
scala> def c(n: Int, l: List[Int]): Seq[(List[Int], List[Int])] = if (n == 0) Seq((Nil, l)) else if (n == l.size) Seq((l, Nil)) else c(n-1, l.tail).map{ case (s,n) => ((l.head :: s), n) } ++ c(n, l.tail).map{ case (s, n) => (s, (l.head :: n)) }
c: (n: Int, l: List[Int])Seq[(List[Int], List[Int])]
scala> def coupon(l: List[Int]) = l.sum match { case x if x >= 1400 => x - 300; case x if x >= 1100 => x - 200; case x if x >= 800 => x - 100 }
coupon: (l: List[Int])Int
scala> c(4, List(299, 649, 529, 519, 269, 649, 259, 219)).minBy{ case (x,y) => coupon(x) - x.min + coupon(y) - y.min }
res1: (List[Int], List[Int]) = (List(299, 269, 259, 219),List(649, 529, 519, 649))