알고리즘/java

<java> 백준 15829 - BigInteger

잼추 2024. 5. 21. 19:01

99클럽 코테 스터디 2일차 / TIL 백준 15829 / BigInteger

 

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

 

해싱이 무엇인지 궁금해서 들어갔다가
브론즈 2 문제 하나만 가볍게 해결해 보기로 했다.

 

50점 까지는 브론즈 답게 아주 쉽게 갔지만
BigInteger를 써보지 않아 100점은 풀지 못해 결국 정답을 참고 하게 되었다.   


BigInteger 


범위와 상관없이 정수를 문자열로 저장하여 안전하게 저장할수 있는 자료 

숫자의 비교

int compare = bigNumber1.compareTo(bigNumber2);


사칙연산

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));


제곱, 최대 공약수, 절대값

num1.pow(2); // 거듭제곱
num1.modPow(num2, new BigInteger("5")); // 거듭 제곱 후 일정 숫자로 나눈값 = 해싱
num1.gcd(num2); // 최대 공약수
num1.abs();


진법 연산 ( n진수 -> 10진수)

String num3 = "100";
BigInteger bi = new BigInteger(num3, 2); //8, num3를 2진수로 인식


10진수-> n진수(참고용!)

String binary = Integer.toBinaryString(num4); // 10진수 -> 2진수
String octal = Integer.toOctalString(num4); // 10진수 -> 8진수
String hexaDecimal = Integer.toHexString(num4); // 10진수 -> 16진수

 

 

문제정답!