https://www.acmicpc.net/problem/2558
내 답안
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int A = s.nextInt();
int B = s.nextInt();
System.out.println(A+B);
}
}
1000번 문제(https://www.acmicpc.net/problem/1000)와는 다른 점
- 1000번 문제는 두 수를 한 줄에 입력받음
- 2558번 문제는 한 줄에 하나의 수를 입력받음
그러나 Scanner 클래스의 nextInt() 메서드는 공백(space)이나 개행(enter)로 입력을 구분한다.
따라서 2558번 문제는 1000번 문제와 동일한 코드로 해결이 가능하다.
만약 1000번 문제와의 입력 양식에서의 차이점을 명확히 하기 위한 코드로 작성하고 싶다면 아래 코드를 참조하면 된다.
[라인별로 처리한 후 형변환해주고자 하는 경우 답안]
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int A, B;
A = Integer.parseInt(sc.nextLine());
B = Integer.parseInt(sc.nextLine());
System.out.println(A+B);
}
}
'Coding Test > 백준' 카테고리의 다른 글
[백준 1330번 - java] 두 수 비교하기 (0) | 2021.11.03 |
---|---|
[백준 9498번 - java] 시험 성적 (0) | 2021.11.03 |
[백준 2753번 - java] 윤년 (0) | 2021.11.03 |
[백준 2588번 - java] 곱셈 (0) | 2021.11.02 |
[백준 1008번 - java] A/B (0) | 2021.11.02 |