Add tags

Add tags to the following comment

こうやって書くと 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))
}

Add tags

The input will be splited to tags with space.

Index

Feed

Other

Link

Pathtraq

loading...