반응형
T에 입력한 숫자만큼 A와 B를 반복해서 돌리는 문제다.
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t;
t = sc.nextInt();
int[] a = new int[t];
int[] b = new int[t];
for(int i=0; i<t; i++){
a[i] = sc.nextInt();
b[i] = sc.nextInt();
}
for(int i=0; i<t; i++){
System.out.println(a[i] + b[i]);
}
sc.close();
}
}
개수만큼 받아야하므로 A와 B를 배열로 선언해야한다.
for문 안에 있는 배열을 [i]로 해야했는데 처음에 [t]로 써서 java.lang.ArrayIndexOutOfBoundsException 오류가 났었다..
출력문은 따로 for문을 써서 해줘야 입력이 완료된 후에 한 번에 출력할 수 있다.
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[백준] 25304 - 영수증 (0) | 2024.02.02 |
---|---|
[백준] 8393 - 합 (0) | 2024.02.01 |
[백준] 2739 - 구구단 (0) | 2024.01.30 |
[백준] 2480 - 주사위 세 개 (0) | 2024.01.29 |
[백준] 2525 - 오븐 시계 (0) | 2024.01.28 |