728x90
문제
https://www.acmicpc.net/problem/10950
두 정수 A와 B를 입력한 뒤, A+B를 한 결과를 출력하는 문제이다.
풀이
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i = 0; i<n;i++) {
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(a+b);
}
}
}
정답은 이렇게 해도 맞은걸로 나오지만, 문제에서 제시한 출력 결과와 다르다.
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int array[] = new int[n];
for(int i = 0; i<n;i++) {
int a = sc.nextInt();
int b = sc.nextInt();
array[i] = a+b;
}
for(int i = 0; i<array.length;i++) {
System.out.println(array[i]);
}
}
}
배열을 이용하면 백준과 똑같은 입력, 출력을 할 수 있다.
728x90
'문제 풀이' 카테고리의 다른 글
[백준] 25304번: 영수증 - JAVA (자바) (0) | 2023.01.21 |
---|---|
[백준] 8393번: 합 - JAVA (자바) (0) | 2023.01.20 |
[백준] 2739번: 구구단 - JAVA (자바) (0) | 2023.01.20 |
[백준] 2480번: 주사위 세개 - JAVA (자바) (0) | 2023.01.19 |
[백준] 2525번: 오븐 시계 - JAVA (자바) (0) | 2023.01.18 |