Created
March 15, 2013 16:01
-
-
Save Wizmann/5170953 to your computer and use it in GitHub Desktop.
瓶子问题
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//瓶子问题 | |
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