Jun's Development Journey

[SWEA] 1233. [S/W 문제해결 기본] Tree - 사칙연산 유효성 검사 본문

SWEA/Intermediate

[SWEA] 1233. [S/W 문제해결 기본] Tree - 사칙연산 유효성 검사

J_Jayce 2019. 8. 7. 10:02

문제

https://www.swexpertacademy.com/main/learn/course/lectureProblemViewer.do

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

www.swexpertacademy.com

 

 

 

풀이

이 문제는 두 자식 노드가 정수이고, 부모 노드가 연산자인지 확인한 후에 부모 노드에 연산 결과로 초기화해주는 과정을 반복하며 유효성을 검사하는 방식으로 구현했다.

 
 
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
import java.util.*;
 
public class Solution {
    static Scanner scan = new Scanner(System.in);
    static String[] nodes;
    static int num;
    static int chk = 1;
    static void solution() {
        for (int i = 1; i <= 10; i++) {
            num = scan.nextInt();
            nodes = new String[num+1];
            scan.nextLine();
            for(int j=1;j<=num;j++) {
                String tmp = scan.nextLine();
                nodes[j] = (tmp.split(" ")[1]);
            }
            test();
            System.out.println("#"+i+" "+chk);
            chk = 1;
        }
    }
    static void test() {
        int idx = num;
        while(idx!=1) {
            if(is_number(idx) && is_number(idx-1&& !is_number(idx/2)) {
                switch(nodes[idx/2]) {
                // 자식 노드가 모두 정수이고 부모가 연산자일 때, 부모의 연산자 요소에 따라 계산 후 부모 노드 초기화
                case "+":{
                    nodes[idx/2= (Integer.parseInt(nodes[idx-1])+Integer.parseInt(nodes[idx])) +"";
                    break;
                }
                case "-":{
                    nodes[idx/2= (Integer.parseInt(nodes[idx-1])-Integer.parseInt(nodes[idx])) +"";
                    break;
                }
                case "*":{
                    nodes[idx/2= (Integer.parseInt(nodes[idx-1])*Integer.parseInt(nodes[idx])) +"";
                    break;
                }
                case "/":{
                    if(nodes[idx].equals("0"))//0으로 나누는 것은 고려하지 않고 유효성만 검사하므로 0 나누는 것이 나오면 1로 임시처리해줌
                        nodes[idx] = "1";
                    nodes[idx/2= (Integer.parseInt(nodes[idx-1])/Integer.parseInt(nodes[idx])) +"";
                    break;
                }
                }
                idx-=2;
            }
            else {
                chk = 0;
                break;
            }
        }
    }
    static boolean is_valid(int idx) {
        if(idx>num)
            return false;
        return true;
    }
    static boolean is_number(int idx) {//숫자와 연산자를 구분하기 위한 함수
        if(!nodes[idx].equals("+"&& !nodes[idx].equals("-"&& !nodes[idx].equals("*"&& !nodes[idx].equals("/"))
            return true;
        return false;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        solution();
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter