Algorithm Thinking
[PS] 백준 1271번
오늘도 타는중
2022. 6. 22. 19:17
💡백준 1271번 : 정수 A와 B를 입력 받아 덧셈 후 출력하기
*Try 1
import java.util.*;
public class B5_1271 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int m;
int n;
m = sc.nextInt();
n = sc.nextInt();
if(m >= 1 && m <= Math.pow(10, 1000) && n >= 1 && n <= Math.pow(10, 1000) ){
System.out.println((m / n));
System.out.println((m % n));
}
sc.close();
}
}
*Search
m과 n의 범위 ≤ 10^1000 이걸 Int 타입에 넣는게 가능한가??
long 타입으로도 불가능. 따라서 큰 정수를 담을 수 있는 방법을 검색하였다.
—> BigInteger를 활용해야하는구나!
■BigInteger 활용법
BigInteger을 초기화하기 위해서는 문자열을 인자 값으로 넘겨주어야 한다. BigInteger가 문자열로 되어 있기 때문.
BigInteger은 문자열 타입이기에 연산이 불가능. 따라서 BigInteger 클래스 내부 메서드 활용.
*Try 2
import java.util.*;
import java.math.BigInteger;
public class B5_1271 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
BigInteger bigNumber = new BigInteger("1000");
BigInteger m = sc.nextBigInteger();
BigInteger n = sc.nextBigInteger();
sc.close();
System.out.println(m.divide(n));
System.out.println(m.remainder(n));
}
}
Success Solving the Problem!
*후기
쉬워보이는 문제도 조건을 보며 하나하나 따져가며 생각을 해야한다고 느꼈다. 갈길이 아직 멀다. 힘내자 💪
*Insight
- 문제풀이 시, 사소한 조건의 중요성
- BigInteger 클래스의 활용법
- 정수를 문자열로도 받을 수가 있고, 범위가 무한대이구나
- Scanner의 close() 메서드의 위치