happy coding

[java] 2738. 행렬 덧셈 본문

coding study/baekjoon

[java] 2738. 행렬 덧셈

yeoonii 2023. 5. 19. 15:52
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int n = Integer.parseInt(st.nextToken());
        int m = Integer.parseInt(st.nextToken());

        int[][] A = new int[n][m];
        int[][] B = new int[n][m];

        for (int i=0;i<n;i++) {
            st = new StringTokenizer(br.readLine());
            for (int j=0;j<m;j++) {
                A[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        for (int i=0;i<n;i++) {
            st = new StringTokenizer(br.readLine());
            for (int j=0;j<m;j++) {
                B[i][j] = Integer.parseInt(st.nextToken());
            }
        }
        StringBuilder sb = new StringBuilder();
        for (int i=0 ; i<n;i++) {
            for (int j=0;j<m;j++) {
                sb.append((A[i][j]+B[i][j]) + " ");
            }
            sb.append("\n");
        }
        System.out.println(sb);
    }
}
import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int m = sc.nextInt();

        int[][] arr = new int[n][m];

        for (int i=0 ; i<2 ; i++) {
            for (int j1=0 ; j1<n ; j1++) {
                for (int j2=0 ; j2<m ; j2++) {
                    arr[j1][j2] += sc.nextInt();
                }
            }
        }
        for (int i=0 ; i<n ; i++) {
            for(int j=0 ; j<m ; j++){
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
}

'coding study > baekjoon' 카테고리의 다른 글

[java] 11654.아스키코드  (0) 2023.07.21
[java] 10871. x보다 작은 수 - scanner  (0) 2023.07.21
[java] 10807.개수 세기  (0) 2023.04.10
[java] 10871.배열  (0) 2023.04.08
[java] 10951.a+b-4  (0) 2023.04.05
Comments