Skip to content

Instantly share code, notes, and snippets.

@notyy
Created April 28, 2012 07:15
Show Gist options
  • Save notyy/2516767 to your computer and use it in GitHub Desktop.
Save notyy/2516767 to your computer and use it in GitHub Desktop.
进制转换
import scala.math._
def toNum(base:Int, src: List[Int]): Int = (base, src) match {
case (_,Nil) => 0
case (b, (x::xs)) => x * pow(b,xs.length).toInt + toNum(b,xs)
}
=======================
scala> toNum(10,List(1,2,3,4))
res7: Int = 1234
scala> toNum(16,List(1,2,3,4))
res8: Int = 4660
scala> toNum(16,List(11,15))
res9: Int = 191
scala> toNum(2,List(1,0,1))
res10: Int = 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment