[백준 알고리즘] 10757번 / 큰 수 A+B (BigInteger)
문제
두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
입력
첫째 줄에 A와 B가 주어진다. (0 < A,B < 1010000)
출력
첫째 줄에 A+B를 출력한다.
입출력 예제
입력 | 출력 |
9223372036854775807 9223372036854775808 | 9 2518446744073709551615 |
🌈 문제 풀이
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
import java.math.BigInteger;
import java.util.Scanner;
public class No10757_Sum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BigInteger a = sc.nextBigInteger();
BigInteger b = sc.nextBigInteger();
sc.close();
System.out.println(a.add(b));
}
}
|
cs |
📢 굉장히 단순한 문제지만 여러 시행착오가 있었다.
문제 제목이 큰 수 더하기여서 그래봤자 Long형이겠거니 하고 변수 선언을 long으로 선언했었다.

그래서 구글링한 결과 BigInteger 라는 클래스의 존재를 처음 알게 되었다!
(반성하자... 2014년부터 전공 공부 시작했고 벌써 2년차 개발자잖아....)
BigInteger 클래스 공부도 할 겸 따로 포스팅 해야지 다짐하면서 문자열을 입력 받아 객체를 생성했다.
BigInteger a = new BigInteger(sc.nextLine());
BigInteger b = new BigInteger(sc.nextLine());
이클립스에서 문제 없이 돌길래 망설임 없이 제출했고! 결과는 참담했다.
NumberFormatException 이 나면서 런타임 에러 😥
뭐가 문제지 하고 찾아보다가, Scanner 클래스에서 nextBigInteger() 메소드를 제공하고 있는 것을 배웠다.
세상은 넓고 개발의 길은 멀고도 험하구나...
BigInteger는 객체이기 때문에 그냥 연산자를 사용할 수 없고,
덧셈을 위해서는 add() 메소드를 사용해야 한다.
쉽지만 공부할 것이 많은 문제였다! 🤓
👩💻 풀어보기 👨💻 https://www.acmicpc.net/problem/10757
10757번: 큰 수 A+B
두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
www.acmicpc.net
https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html
BigInteger (Java Platform SE 7 )
Immutable arbitrary-precision integers. All operations behave as if BigIntegers were represented in two's-complement notation (like Java's primitive integer types). BigInteger provides analogues to all of Java's primitive integer operators, and all relevan
docs.oracle.com