运行时错误-Google Code Jam 2019 Qualifier-Scala

问题描述 投票:0回答:1

我正在尝试在2019年Google Code Jam资格回合归档中向Foregone解决方案提交解决方案。我是Scala的新手。我的代码通过IntelliJ运行完成。当我将以下代码提交到Google Code Jam服务器时,有人知道导致运行时错误的原因吗?

import scala.io.StdIn
object main extends App {
  val T = StdIn.readInt
  var firstCheck: String = ""
  var secondCheck: String = ""
  var N: String = ""
  for (i <- 1 to T) {
    N = StdIn.readLine()
    for (j <- 0 until N.length) {
      if (N(j) == '4') secondCheck += "1"
      else if (secondCheck != "") secondCheck += "0"
    }
    firstCheck = N.replaceAll("[4]", "3")
    println(s"Case #$i: $firstCheck $secondCheck")
    secondCheck = ""
  }
}
scala
1个回答
0
投票

我不知道为什么Code Jam可能会引发运行时错误,但是简化算法可能会避免此问题。

val (frst,scnd) = StdIn.readLine().foldLeft(("","")){
  case ((a,b),'4') => (a + '3', b + '1')
  case ((a,""), c) => (a + c, "")
  case ((a, b), c) => (a + c, b + '0')
}
println(s"Case #$i: $frst $scnd)
© www.soinside.com 2019 - 2024. All rights reserved.