배열 한접시

[백준] 1475번: 방 번호 [Java]

NaZZU 2024. 4. 18. 22:18

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.Scanner;
 
public class Main{
 
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
 
        int input = sc.nextInt();
        
        int[] arr = new int[10];
        
 
        while (input != 0)
        {
            int num = input % 10;
            
            if (num == 9 && arr[num] > arr[6])
                arr[6]++;
            else if(num == 6 && arr[num] > arr[9])
                arr[9]++;
            else
                arr[num]++;
            
            input /= 10;
        }
        
        int max = 0;
        for (int i : arr)
            max = Math.max(max,  i);
        
        System.out.println(max);
    }
 
}
 
cs

 

9와 6을 돌려쓸 수 있는 조건이 달려있는 문제다.

어떻게 하면 효율적으로 풀 수 있을지 고민하다 그냥 9번방(혹은 6번방)의 값이 반대쪽 방의 값보다 크다면 반대쪽 방의 값을 증가시키고, 아니면 자신의 방의 크기를 증가시키는 방법을 사용했다.