我正在尝试解决UVA在线判断问题,380 - 呼叫转移https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=316
具体来说,我已经设法创建了一个函数,它从问题描述中的示例中给出了正确的答案,我继续编写了包含所有测试用例的文件读取过程,处理每个测试用例的答案并回写结果到文件系统,问题是我可以读取整个文件但由于某种原因有一个特殊的核心功能(“回写”),在计算时没有被调用,但编译器确实编译,我已经查看了代码几次,但我找不到问题,这是我的代码:
type userSettings = List[Req]
type callSimulations = List[Array[String]]
type Juice = (userSettings, callSimulations)
def calling(time: Double, ext: String, lst: List[Req], nineNine: Set[String]):String = {
if (nineNine contains ext) "9999"
else {
val next = lst.filter(req => req.source == ext && (time >= req.time2 && time <= req.limit))
println(s"time: $time, ext: $ext and next: $next ")
if (next == Nil) s"RINGS $ext \n"
else calling(time, next(0).target, lst, nineNine + ext)
}
}
def getData(input: Iterator[String]): Unit = {
def writeBack(theSys: Juice, sysNum: Int, output: String): String = {
val heading = "System $sysNum \n"
val (uSettings, sim) = theSys // unpack the the system
val partialResult = sim.map {arr =>
"AT " + arr(0) + "CALL " + "TO " + arr(1) + calling(parse(arr(0)),arr(1),uSettings,Set[String]())}.mkString("")
println(heading + partialResult)// doesnt print anything so it's not writting
output + heading + partialResult
}
def rev(x: Juice) = (x._1.reverse, x._2.reverse)
@annotation.tailrec
def go(in: Iterator[String], acc: Juice, out:String, n: Int): String = {
if (in.hasNext) {
val next = in.next.split(" ")
next match{
case Array(a,b,c,d) => go(in,(Req(a,b,c,d) +: acc._1, acc._2), out, n)
case Array(time,ext) => go(in,(acc._1, Array(time,ext) +: acc._2), out, n)
case Array(cut) if n == "9000" => go(in, (Nil,Nil), writeBack(rev(acc), n , out), n+1)
case _ => go(in, acc, out, n)
}
} else out
}
val zero = (Nil,Nil)
val outFile = new File("path/of/answers.txt")
if (!outFile.exists()) outFile.createNewFile()
val bw = new BufferedWriter(new FileWriter(outFile,true))
bw.write( go(input,zero,"",0) )
bw.close
}
val file = "path/to/inputFile.txt"
val theIn = io.Source.fromFile(file)
try getData(theIn.getLines) finally theIn.close
type userSettings = List[Req]
type callSimulations = List[Array[String]]
type Juice = (userSettings, callSimulations)
def calling(time: Double, ext: String, lst: List[Req], nineNine: Set[String]):String = {
if (nineNine contains ext) "9999"
else {
val next = lst.filter(req => req.source == ext && (time >= req.time2 && time <= req.limit))
println(s"time: $time, ext: $ext and next: $next ")
if (next == Nil) s"RINGS $ext \n"
else calling(time, next(0).target, lst, nineNine + ext)
}
}
def getData(input: Iterator[String]): Unit = {
def writeBack(theSys: Juice, sysNum: Int, output: String): String = {
val heading = "System $sysNum \n"
val (uSettings, sim) = theSys // unpack the the system
val partialResult = sim.map {arr =>
"AT " + arr(0) + "CALL " + "TO " + arr(1) + calling(parse(arr(0)),arr(1),uSettings,Set[String]())}.mkString("")
println(heading + partialResult)// doesnt print anything so it's not writting
output + heading + partialResult
}
def rev(x: Juice) = (x._1.reverse, x._2.reverse)
@annotation.tailrec
def go(in: Iterator[String], acc: Juice, out:String, n: Int): String = {
if (in.hasNext) {
val next = in.next.split(" ")
next match{
case Array(a,b,c,d) => go(in,(Req(a,b,c,d) +: acc._1, acc._2), out, n)
case Array(time,ext) => go(in,(acc._1, Array(time,ext) +: acc._2), out, n)
case Array(cut) if n == "9000" => go(in, (Nil,Nil), writeBack(rev(acc), n , out), n+1)
case _ => go(in, acc, out, n)
}
} else out
}
val zero = (Nil,Nil)
val outFile = new File("path/of/answers.txt")
if (!outFile.exists()) outFile.createNewFile()
val bw = new BufferedWriter(new FileWriter(outFile,true))
bw.write( go(input,zero,"",0) )
bw.close
}
val file = "path/to/inputFile.txt"
val theIn = io.Source.fromFile(file)
try getData(theIn.getLines) finally theIn.close
在此阶段,req或parse的实现无关紧要。
我没有直接向网站提交我的代码以便它可以处理所有I / O的原因是因为我使用的是Scala并且该网站不接受该语言,因此我使用的是一个名为udebug,人们可以下载一些测试用例并比较输出。
我该怎么办?