N
*M
크기의 두 행렬 A와 B가 주어졌을 때, 두 행렬을 더하는 프로그램을 작성하시오.
첫째 줄에 행렬의 크기 N
과 M
이 주어진다. 둘째 줄부터 N
개의 줄에 행렬 A의 원소 M
개가 차례대로 주어진다. 이어서 N
개의 줄에 행렬 B의 원소 M
개가 차례대로 주어진다. N
과 M
은 100보다 작거나 같고, 행렬의 원소는 절댓값이 100보다 작거나 같은 정수이다.
첫째 줄부터 N
개의 줄에 행렬 A와 B를 더한 행렬을 출력한다. 행렬의 각 원소는 공백으로 구분한다.
3 3
1 1 1
2 2 2
0 1 0
3 3 3
4 4 4
5 5 100
4 4 4
6 6 6
5 6 100
-문제의 오타를 찾은 사람: purpose
import java.util.Scanner;
public class Code2738 {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int first=0;
int second=0;
first=scanner.nextInt();
second=scanner.nextInt();
int[][] farray=new int[first][second];
int[][] sarray=new int[first][second];
int[][] tarray=new int[first][second];
for(int i=0;i<first;i++) {
for(int j=0;j<second;j++) {
farray[i][j]=scanner.nextInt();
}
}
for(int i=0;i<first;i++) {
for(int j=0;j<second;j++) {
sarray[i][j]=scanner.nextInt();
}
}
for(int i=0;i<first;i++) {
for(int j=0;j<second;j++) {
tarray[i][j]=farray[i][j]+sarray[i][j];
}
}
for(int i=0;i<first;i++) {
for(int j=0;j<second;j++) {
System.out.print(tarray[i][j]+" ");
}
System.out.println("");
}
}
}
바로 덧셈하면서 출력했더니 값이 이상하게 나와서
다 저장해놓고 다시 돌렸다..