[백준(BOJ)] 1271번 : 엄청난 부자2 - JAVA

김무영·2025년 1월 9일

백준 알고리즘

목록 보기
1/2

1271번 : 엄청난 부자2


문제 접근

첫 접근 방법으로는 값 두개를 int로 받아서 "/" , "%" 로 결과를 출력하려고 하였다.
그러곤 런타임 에러 (InputMismatch)의 반복
조건을 다시 살펴보니 입력값은 1보다 크거나 같고 10^1000보다 작거나 같다.

int의 범위 (-2^31 ~ (2^31)-1)를 초과하는 입력값을 가질 수 있고
long의 범위 (-2^63 ~ (2^63)-1)로도 처리가 불가능한 값이 존재한다.

BigInteger은 이론상 무제한의 값을 저장할 수 있다는 것을 알게 되었다.


코드

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        BigInteger first = sc.nextBigInteger();
        BigInteger second = sc.nextBigInteger();

        BigInteger first_answer = first.divide(second);
        BigInteger second_answer = first.remainder(second);

        System.out.print(first_answer +"\n" +second_answer);

    }
}

BigInteger

BigIntger은 문자열 형식으로 되어있기 때문에 사칙연산을 수행할 수 없다.

"+" : add()
"-" : subtract()
"*" : multiply()
"/" : divide()
"%" : remainder()


1개의 댓글

comment-user-thumbnail
2025년 1월 14일

BigInteger.intValue() -> int
BigInteger.longValue() -> long
BigInteger.floatValue() -> float
BigInteger.doubleValue() ->double

답글 달기