뭉지(moonz) 2021. 9. 8. 08:49
반응형

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

 

1271번: 엄청난 부자2

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

www.acmicpc.net

 

브론즈 5 문제이다.

 

BigInteger

  • int(약 -+20억, -2,147,483,648 ~ 2,147,483,647)
    long(-9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807)보다 더 큰 범위의 타입
  • 무한의 정수가 들어갈 수 있는 가능성이 있다면 BigInteger이라는 클래스를 활용하는 것이 좋음
  • 알고리즘 문제에서 최악의 경우를 대비하고자 사용하기도 함
  • BigInteger을 초기화하려면 문자열을 인자값으로 넘겨주어야함
  • 문자열이기에 사칙연산 불가능
  • BigInteger 내부의 숫자계산은 BigInteger 클래스 내부에 있는 메서드 사용!
    • + : add
    • - : subtract
    • * : multiply
    • / : divide
    • % : remainder, mod

 

전체 코드

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

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        BigInteger N = sc.nextBigInteger();
        BigInteger M = sc.nextBigInteger();
        BigInteger result = new BigInteger("1000");
        BigInteger result2 = new BigInteger("1000");
        result = N.divide(M);
        result2 = N.mod(M);		//remainder
        
        System.out.println(result);
        System.out.println(result2);
    }

 

 

+) BigInteger 사용 문제  : 2338번: 긴자리 계산 (acmicpc.net)

반응형