본문 바로가기
✨ 코딩테스트/Baekjoon

[Baekjoon / JAVA] 백준 1271번 엄청난 부자2

by 환풍 2023. 2. 12.
728x90

 

 

 

1271번: 엄청난 부자2

첫째 줄에는 최백준 조교가 가진 돈 n과 돈을 받으러 온 생명체의 수 m이 주어진다. (1 ≤ m ≤ n ≤ 101000, m과 n은 10진수 정수)

www.acmicpc.net

 

 

처음엔 이렇게 에러가떴다. 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import com.sun.source.tree.CaseTree;
import java.math.*;
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
 
        Scanner sc = new Scanner(System.in);
 
        int a = sc.nextInt();
        int b= sc.nextInt();
 
        System.out.println(a/b);
        System.out.println(a%b);
 
    }
}
 
cs

My code

위와 같은 코드를 이용하면 런타임 에러가 뜬다.

브론즈5 문제이지만 정답률이 낮은 이유는 아무래도 int형이나 long형의 범위를 초과하는 숫자가 입력으로 주어지기 때문이다. 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import com.sun.source.tree.CaseTree;
import java.math.*;
import java.util.Scanner;
 
public class Main {
    public static void main(String[] args) {
 
        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));
 
    }
}
 
cs

HOW

그래서 이 경우 나는 BigInteger를 이용해주었다.

반응형

댓글