Notice
Recent Posts
Recent Comments
Link
«   2025/01   »
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
Archives
Today
Total
관리 메뉴

최's 먹공로그

SW expert Academy 1233_[S/W 문제해결 기본]9일차_사칙연산 유효성 검사 본문

APS

SW expert Academy 1233_[S/W 문제해결 기본]9일차_사칙연산 유효성 검사

ChoiSH313 2019. 2. 4. 12:42

https://www.swexpertacademy.com/main/code/problem/problemDetail.do

 

 

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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
 
public class Solution_1233_SW문제해결기본9일차_사칙연산유효성검사_최성호2 {
 
    static String hap = "";
 
    public static void main(String[] args) throws NumberFormatException, IOException {
 
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 
        for (int i = 1; i <= 10; i++) {
            hap = "";
            char[] list2 = new char[1000];
            int n = Integer.parseInt(br.readLine());
            for (int j = 1; j <= n; j++) {
 
                String str = br.readLine();
                StringTokenizer st = new StringTokenizer(str, " ");
 
                while (st.hasMoreTokens()) {
 
                    int node = 0;
                    char oper = 0;
                    int ch1 = 0;
                    int ch2 = 0;
 
                    // 입력이 2,3,4개 입력되는 경우로 되어있음
                    if (st.countTokens() == 4) {
                        node = Integer.parseInt(st.nextToken());
                        oper = st.nextToken().charAt(0);
                        ch1 = Integer.parseInt(st.nextToken());
                        ch2 = Integer.parseInt(st.nextToken());
                    } else if (st.countTokens() == 2) {
                        node = Integer.parseInt(st.nextToken());
                        oper = st.nextToken().charAt(0);
                    }
                    else if(st.countTokens() == 3) {
                        node = Integer.parseInt(st.nextToken());
                        oper = st.nextToken().charAt(0);
                        ch1 = Integer.parseInt(st.nextToken());
                    }
 
                    // 1번 노드에는 그냥 저장하고 끝
                    if (node == 1) {
                        list2[1= oper;
                        break;
                    }
                    
                    // 1번 노드가 아닌경우 왼쪽 자식노드가 비어있으면 왼쪽 자식노드에 먼저 넣어줌
                    for (int k = 1; k < list2.length; k++) {
                        if (k * 2 < 1000 && list2[k * 2== 0) { // 왼쪽 자식노드 비어있음
                            list2[k * 2= oper;
                            break;
                        } else if (k * 2 + 1 < 1000 && list2[k * 2 + 1== 0) { // 왼쪽 자식노드가 있음
                            list2[k * 2 + 1= oper;
                            break;
                        }
                    } // end of k
                } // end of while
            } // end of n
 
            // 중위표기법으로!!
            inOrder(list2, 1);
            
            // 중위표기법 완성하고 출력 조건
            int cnt = 0;
            for (int i1 = 1; i1 < hap.length(); i1++) {
                // 현재 값이 연산자이면서
                if (hap.charAt(i1) == '+' || hap.charAt(i1) == '-' || hap.charAt(i1) == '/' || hap.charAt(i1) == '*') {
                    // 그 전 값도 연산자 였으면 제대로 된 수식이 아니므로 0출력하고 cnt++
                    if (hap.charAt(i1 - 1== '+' || hap.charAt(i1 - 1== '-' || hap.charAt(i1 - 1== '/'
                            || hap.charAt(i1 - 1== '*') {
                        System.out.println("#"  + i+" " + 0);
                        cnt++;
                        break;
                    }
                // 현재 값이 숫자이면서
                } else {
                    // 그 전 값이 연산자 였으면 제대로 된 수식이므로 continue
                    if (hap.charAt(i1 - 1== '+' || hap.charAt(i1 - 1== '-' || hap.charAt(i1 - 1== '/'
                            || hap.charAt(i1 - 1== '*') {
                        continue;
                    }
                    // 그 전 값이 숫자였으면 잘못된 수식이므로 0출력하고 cnt++
                    else {
                        System.out.println("#"  + i+" " + 0);
                        cnt++;
                        break;
                    }
                }
            }
            // 위 조건이 끝나면 제대로 된 수식이므로 cnt=0인 상태
            if (cnt == 0) {
                System.out.println("#"  + i+" " + 1);
            }
 
        } // end of tc
    } // end of main
 
    // 트리의 노드에 있는 값들을 중위표기법으로 1+2*3...이런식으로 꺼내줌
    public static void inOrder(char[] arr, int index) {
        // 왼쪽자식
        if (index * 2 < arr.length && arr[index * 2!= 0) {
            inOrder(arr, index * 2);
        }
        hap += arr[index]; // hap에 문자열로 1+2*3...더해짐
        // 오른쪽 자식
        if (index * 2 + 1 < arr.length && arr[index * 2 + 1!= 0) {
            inOrder(arr, index * 2 + 1);
        }
    }
 
}
 
cs