728x90

https://www.acmicpc.net/problem/1271

 

1271번: 엄청난 부자2

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

www.acmicpc.net

 

 

풀이)

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

public class Main_BJ_1271_엄청난부자2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        BigInteger bigNum1 = new BigInteger(sc.next());
        BigInteger bigNum2 = new BigInteger(sc.next());

        System.out.println(bigNum1.divide(bigNum2));
        System.out.println(bigNum1.remainder(bigNum2));

    }//main
}

BigInteger를 연습하기 위해 간단한 문제를 찾았다.

BigInteger는 long보다 큰 수를 담기에 좋다. 또한 문자열을 넣어주기 때문에 더 편함!

 

대신, java.math를 import 해줘야하고, 사칙연산을 하는 방법이 다르므로 참고!!

 

 

https://coding-factory.tistory.com/604

 

[Java] 큰 숫자(정수) 다루기 BigInteger 사용법 & 예제 총정리

BigInteger를 사용해야 하는 이유 Type 범위 int -2,147,483,648 ~ 2,147,483,647 long -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 int는 메모리 크기는 4byte로 표현할 수 있는 범위는 -2,147,483,648 ~ 2,147,483,647이고 long

coding-factory.tistory.com

이 블로그를 참고하면서 BigInteger에 익히는 계기가 되었다!

728x90

+ Recent posts