본문 바로가기

프로그래밍/Scala

백준 2738번 행렬의 덧셈

 

 

 

 

import scala.io.StdIn

object Main {
  def main(args: Array[String]): Unit = {
    val input = StdIn.readLine().split(" ")
    val N = input(0).toInt
    val M = input(1).toInt

    var A = Array.ofDim[Int](N,M)
    var B = Array.ofDim[Int](N,M)
    var C = Array.ofDim[Int](N,M)

    var i: Int = 0
    var j: Int = 0

    i = 0;
    while (i < N) {
      val input2 = StdIn.readLine().split(" ")
      j = 0;
      while (j < M) {
        A(i)(j) = input2(j).toInt
        j = j + 1
      }
      i = i + 1
    }

    i = 0;
    while (i < N) {
      val input2 = StdIn.readLine().split(" ")
      j = 0
      while (j < M) {
        B(i)(j) = input2(j).toInt
        j = j + 1
      }
      i = i + 1
    }

    for(i<-0 until N) {
      for(j<-0 until M) {
        print(A(i)(j) + B(i)(j)+" ")
      }
      println()
    }
  }
}

 

 

import scala.io.StdIn

object Main {
  def main(args: Array[String]): Unit = {
    val input = StdIn.readLine().split(" ")
    val N = input(0).toInt
    val M = input(1).toInt

    var A = Array.ofDim[Int](N,M)
    var B = Array.ofDim[Int](N,M)
    var C = Array.ofDim[Int](N,M)

    var i: Int = 0
    var j: Int = 0

    i = 0;
    while (i < N) {
      val input2 = StdIn.readLine().split(" ")
      j = 0;
      while (j < M) {
        A(i)(j) = input2(j).toInt
        j = j + 1
      }
      i = i + 1
    }

    i = 0;
    while (i < N) {
      val input2 = StdIn.readLine().split(" ")
      j = 0
      while (j < M) {
        B(i)(j) = input2(j).toInt
        j = j + 1
      }
      i = i + 1
    }

    for(i<-0 until N) {
      for(j<-0 until M) {
        print(A(i)(j) + B(i)(j)+" ")
      }
      println()
    }
  }
}

 

import scala.io.StdIn.{readLine, readInt}

object Main:
  def main(args: Array[String]): Unit =
    val (n, m) = readLine().split(" ").map(_.toInt) match
      case Array(n, m) => (n, m)

    val a =
      for _ <- 0 until n
      yield readLine().split(" ").map(_.toInt)

    val b =
      for _ <- 0 until n
      yield readLine().split(" ").map(_.toInt)

    val c =
      for (arow, brow) <- a.zip(b)
      yield for (acol, bcol) <- arow.zip(brow)
      yield acol + bcol

    for crow <- c
    do
      for ccol <- crow
      do print(ccol + " ")
      println()

 

 

 

 

 

 

 

 

 

'프로그래밍 > Scala' 카테고리의 다른 글

Scala - Type polymorphism  (0) 2022.11.26
Scala Array 문법  (0) 2022.11.24
Scala Array 문법  (0) 2022.11.22
Scala String 문법  (0) 2022.11.22
1103 Mixin, Stacking with Traits  (0) 2022.11.03