Created
April 14, 2017 12:54
-
-
Save waynejo/e3714d8545f2c51de71abe7caf81f9be to your computer and use it in GitHub Desktop.
This file contains 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
package Main | |
import java.io.{FileInputStream, FileOutputStream} | |
import scala.io.StdIn | |
object Main2 extends App { | |
Console.setIn(new FileInputStream("example2.in")) | |
Console.setIn(new FileInputStream("B-small-practice.in")) | |
Console.setOut(new FileOutputStream("B-small-practice.out")) | |
Console.setIn(new FileInputStream("B-large-practice.in")) | |
Console.setOut(new FileOutputStream("B-large-practice.out")) | |
def solve(number: Long, last: Long = 0): Long = { | |
if (last == number) { | |
number | |
} else { | |
val numbers = number.toString.toCharArray | |
def _solve(idx:Int = 0, acc: List[Char] = List()): List[Char] = { | |
val c = numbers(idx) | |
if (idx + 1 >= numbers.length) { | |
acc :+ numbers(idx) | |
} else { | |
if (c > numbers(idx + 1)) { | |
(acc :+ (c - 1).toChar) ++ ("9" * (numbers.length - idx - 1)).toCharArray | |
} else { | |
_solve(idx + 1, acc :+ c) | |
} | |
} | |
} | |
solve(_solve().mkString.toLong, number) | |
} | |
} | |
val cases = StdIn.readLine().toInt | |
(1 to cases) foreach { i => { | |
val number = StdIn.readLine().toLong | |
println(s"Case #$i: ${solve(number)}") | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment