Skip to content

Instantly share code, notes, and snippets.

@Wizmann
Created March 15, 2013 16:01
Show Gist options
  • Save Wizmann/5170953 to your computer and use it in GitHub Desktop.
Save Wizmann/5170953 to your computer and use it in GitHub Desktop.
瓶子问题
//瓶子问题
import scala.annotation.tailrec
object Bottle
{
val N_Bottle = 3
def main(args : Array[String])
{
val n = readLine toInt
@tailrec
def solve(full : Int, empty : Int = 0, drinked : Int = 0) : Int =
{
if(full != 0)
{
solve(0, empty+full, drinked+full)
}
else if(empty < N_Bottle)
{
drinked
}
else
{
solve(empty / N_Bottle, empty % N_Bottle, drinked)
}
}
println(solve(n))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment