컴퓨터 프로그래밍/알고리즘

[알고리즘] Baekjoon Bronze 5

한33 2025. 1. 10. 17:07

Baekjoon Bronze 5 를 풀면서 알게된 작은 정보들 메모

 

💡 Int 타입의 범위

데이터 범위가 -2,147,483,648 ~ 2,147,483,647 안에 있을 때. int 를 사용, 넘어가면 long 타입을 쓰자

 

💡 length

String 에 뒤에 .length() 를 쓰면 문자열의 길이를 알 수 있다.

 

💡 BigInteger 를 활용한 큰 수의 덧셈 A.add(B)

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    BigInteger A = sc.nextBigInteger();
    BigInteger B = sc.nextBigInteger();
    System.out.println(A.add(B));
}

 

10의 10000승과 같이 엄청 큰 수를 더할 때는 BigInteger 를 사용한다.

BigInteger 를 더할 때는 .add 메서드를 활용한다.

 

💡 Integer.parseInt()

Integer.parseInt() 를 통해 문자를 int 타입으로 바꿀 수 있다.

 

💡 문자열의 n 번 째 문자 가져오기

text.substring(0, 1) = text.charAt(0) 

이런식으로 문자열의 첫 번째 문자를 가져올 수 있다.

 

text.substring(text.length() -1) 로 마지막 문자를 가져올 수 있다.

 

💡 날짜 가져오기

Date date = new Date();
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("YYYY-MM-dd");
System.out.println(simpleDateFormat.format(date));

 

Date 를 가져와서 원하는 포맷으로 바꿔서 오늘의 날짜를 출력할 수 있다.

 

💡 Math.abs() 절댓값

Math.abs(N-M)

 

위 방법으로 절댓값을 알 수 있다.