Skip to content

Instantly share code, notes, and snippets.

@daclouds
Last active August 29, 2015 14:14
Show Gist options
  • Save daclouds/2ab8e9915f498863bf03 to your computer and use it in GitHub Desktop.
Save daclouds/2ab8e9915f498863bf03 to your computer and use it in GitHub Desktop.
import java.io.{FileReader, BufferedReader}
object TheNextNumber extends App {
val input = new BufferedReader(new FileReader("B-small-practice.in"))
val T: Int = input.readLine().toInt
def nextNumber(s: String): String = {
for (i <- (s.length-1) to 0 by -1) {
if (s(i) > s(i-1)) {
val front = s.substring(0, i-1)
val arr = s.substring(i).toList
val mid = arr.filter(j => s(i-1) < j).min
val idx = arr.indexOf(mid)
val end = arr.updated(idx, s(i-1)).sorted.mkString
return front + mid + end
}
}
s
}
for (t <- 0 until T) {
val num = "0" + input.readLine()
val result = BigInt(nextNumber(num))
println(f"Case #${t + 1}: ${result}")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment