ddubi

[백준/java] 2738번 행렬 덧셈 본문

코테 문제풀이

[백준/java] 2738번 행렬 덧셈

ddubi__ 2022. 12. 7. 20:09

https://www.acmicpc.net/problem/2738

 

2738번: 행렬 덧셈

첫째 줄에 행렬의 크기 N 과 M이 주어진다. 둘째 줄부터 N개의 줄에 행렬 A의 원소 M개가 차례대로 주어진다. 이어서 N개의 줄에 행렬 B의 원소 M개가 차례대로 주어진다. N과 M은 100보다 작거나 같

www.acmicpc.net


➡️ 풀이

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class back_2738 { // 행렬 덧셈
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String[] data = br.readLine().split(" ");
        int N = Integer.parseInt(data[0]);
        int M = Integer.parseInt(data[1]);
        int[][] answer = new int[N][M];

        for(int i=0; i<N; i++){
            String[] temp = br.readLine().split(" ");
            for(int j=0; j<M; j++){
                answer[i][j] += Integer.parseInt(temp[j]);
            }
        }
        for(int i=0; i<N; i++){
            String[] temp = br.readLine().split(" ");
            for(int j=0; j<M; j++){
                answer[i][j] += Integer.parseInt(temp[j]);
            }
        }
        for(int i=0; i<answer.length; i++){
            for(int j=0; j<answer[i].length; j++){
                System.out.print(answer[i][j] + " ");
            }
            System.out.println();
        }
    }
}

이렇게 푸는게 맞나 싶다...

더 좋은 방법이 있을거라 생각함..

Comments