14720 우유축제

2020. 2. 3. 14:50Learn/Algorithm

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
36
37
38
39
40
import java.util.Scanner;
 
public class Main {
    
    
    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int ar[] = new int [1001];
        int ar2[] = new int [1001];
        boolean al = false;
        int cnt = 0;
        int temp = 0;
        int maxim = 0;
        for(int i = 0; i < a; i++) {
            ar[i] = sc.nextInt();
        }
        for(int i = 0; i < a; i++) {
            if(ar[i] == 0 && al == false) {
                temp = 1;
                cnt++;
                al = true;
            }
            if(ar[i] == 1 && al == true && temp == 1) {
                cnt++;
                temp = 2;
            }
            if(ar[i] == 2 && al == true && temp == 2) {
                cnt++;
                temp = 3;
            }if(ar[i] == 0 && al == true && temp == 3) {
                temp = 1;
                cnt++;
            }
        }
        System.out.println(cnt);
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs

예외처리해야 하는 case는

첫번째 0, 0일때 1이  올때, 1일때 2올때, 2일때 0이 올때

세 가지 경우를 각각 예외처리 해주었다.

'Learn > Algorithm' 카테고리의 다른 글

2667 단지번호붙이기  (0) 2020.02.04
1260 DFS 와 BFS - 백준  (0) 2020.02.04
1003번 피보나치 함수  (0) 2020.02.03
2869번 달팽이는 올라가고 싶다.  (0) 2020.02.03
2447 별찍기 -10  (0) 2020.02.02