알고리즘/백준
10757번 문제 : 큰 수 A+B
son_i
2023. 5. 5. 20:36
728x90
10757번: 큰 수 A+B
두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
www.acmicpc.net
자바
int는 2의 21승까지 표현 가능하니까 택도 없다. (-2,147,483,648 ~ 2,147,483,647)
long형은 8바이트로 2의 64 승이지만 이것도 택도 없다. (-9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807)
그래서 찾아보니까 BigInteger라는 자료형이 있다는 것을 알게되었다 !
- BigInteger은 문자열 형태로 이루어져 있어 숫자의 범위가 무한.
선언 : BigInteger num = new BigInteger("qwe4rt");
계산 : System.out.println(A.add(B));
//문자열 타입이지만 BigInteger A = st.nextToken(); 으로 넣어주니까 안 됐다.
new로 객체를 생성하고 그 안에 nextToken()으로 초기화 해주니까 됐다.
관련 내용 Notion에 정리 !
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.StringTokenizer;
class Main{
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
BigInteger A = new BigInteger(st.nextToken());
BigInteger B = new BigInteger(st.nextToken());
System.out.println(A.add(B));
}
}
728x90