알고리즘/백준
[백준] 1271번 - 엄청난 부자 2
땀두
2022. 3. 20. 12:12

이 문제를 보고 쉽게 그냥 나누기와 모듈러 연산이면 되겠다고 생각하고 아래와 같이 문제를 해결했다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class p1271 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a = Integer.parseInt(br.readLine());
int b = Integer.parseInt(br.readLine());
System.out.println(a/b);
System.out.println(a%b);
}
}
하지만 런타임 에러가 났고, 문제를 자세히 보니 인풋 값이 10^1000 까지인 굉장히 큰 수였다. 이러한 수는 int나 long 등으로 표현할 수 없어 검색해보니 BigInteger 라는 형식이 있어 이를 이용하여 문제를 풀었다.
BigInteger는 일반적인 사칙연산을 할 수 없어 직접 구현된 내부함수를 사용하였다.
import java.math.BigInteger;
import java.util.Scanner;
public class p1271 {
public static void main(String[] args){
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
BigInteger a = sc.nextBigInteger();
BigInteger b = sc.nextBigInteger();
System.out.println(a.divide(b));
System.out.println(a.mod(b));
}
}