반응형
https://www.acmicpc.net/problem/10757
10757번: 큰 수 A+B
두 정수 A와 B를 입력받은 다음, A+B를 출력하는 프로그램을 작성하시오.
www.acmicpc.net
생각보다 간단한 문제이지만 int범위를 생각하면 쉽지 않은 문제이다. 처음에 double형으로 바꾸고 casting을 통해 출력하려 했지만 double과 long을 뛰어넘는 숫자라 java math.BigInteger;를 import를 해 문제를 해결하였다.
BigInteger란
System.out.println("덧셈(+) :" +bigNumber1.add(bigNumber2));
System.out.println("뺄셈(-) :" +bigNumber1.subtract(bigNumber2));
System.out.println("곱셈(*) :" +bigNumber1.multiply(bigNumber2));
System.out.println("나눗셈(/) :" +bigNumber1.divide(bigNumber2));
System.out.println("나머지(%) :" +bigNumber1.remainder(bigNumber2));
라는 형식을 가지고 String을 통해서 연산을 할 수 있고
Biginteger . valueOf()를 통해 형변환이 가능하다.
import java.io.IOException;
import java.math.BigInteger;
import java.util.*;
public class java {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
BigInteger bigInteger = new BigInteger(sc.next());
BigInteger bigInteger2 = new BigInteger(sc.next());
System.out.println(bigInteger.add(bigInteger2));
}
}
반응형
'코테 > 백준' 카테고리의 다른 글
태태개발일지 - 백준 (0) | 2024.10.15 |
---|---|
N13223 (1) | 2023.05.17 |
달팽이는 올라가고싶다 -2869 (0) | 2023.02.23 |
기본수학 -벌집 (0) | 2023.02.22 |