Comment detail
情報オリンピック2006年度国内本選問題1 (Nested Flatten)This comment is reply for 5566 yukoba: 中高生向けの情報オリンピックの国内本選2...(情報オリンピック2006年度国内本選問題1). Go to thread root.
こうやって書くと var をなくせるんですね。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | object Hon1 extends Application {
//Console.setIn(new java.io.FileReader("input.txt"))
val ary = Console.readLine().split(" ")
val n = Integer.parseInt(ary(0))
val k = Integer.parseInt(ary(1))
val a = List.tabulate(n, i => Console.readInt).toArray
val firstSum = a.take(k).reduceLeft((i:int,j:int) => i + j)
def calc(i:int, prevSum:int, maxSum:int):int = {
if(i == n) return maxSum
val curSum = prevSum - a(i - k) + a(i)
return calc(i + 1, curSum, Math.max(maxSum, curSum))
}
println(calc(k, firstSum, firstSum))
}
|



yukoba #5571() [ Scala ] Rating0/0=0.00
入出力は標準入出力にて。
object Hon1 extends Application { //Console.setIn(new java.io.FileReader("input.txt")) val ary = Console.readLine().split(" ") val n = Integer.parseInt(ary(0)) val k = Integer.parseInt(ary(1)) val a = List.tabulate(n, i => Console.readInt).toArray var firstSum = 0 a.take(k).foreach(v => firstSum += v) def calc(i:int, prevSum:int, maxSum:int):int = { if(i == n) return maxSum val curSum = prevSum - a(i - k) + a(i) return calc(i + 1, curSum, Math.max(maxSum, curSum)) } println(calc(k, firstSum, firstSum)) }Rating0/0=0.00-0+