-
-
Save daclouds/2ab8e9915f498863bf03 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
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