-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path1232_사칙연산.cpp
46 lines (42 loc) · 1.27 KB
/
1232_사칙연산.cpp
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
#include<iostream>
using namespace std;
int n;
int tree[1001][3]; // 연산자 or 숫자, left, right
int calc(int v) {
if (tree[v][0] < 0) {
int a = calc(tree[v][1]);
int b = calc(tree[v][2]);
if (tree[v][0] == -1) return a + b;
else if (tree[v][0] == -2) return a - b;
else if (tree[v][0] == -3) return a * b;
else if (tree[v][0] == -4) return a / b;
}
return tree[v][0];
}
int main(int argc, char** argv)
{
int T = 10;
for (int test_case = 1; test_case <= T; ++test_case) {
cin >> n;
for (int i = 1; i <= n; i++) {
char ch;
scanf("%d %c", &i, &ch);
if (ch >= '0' && ch <= '9') {
tree[i][0] = 0;
while (ch >= '0' && ch <= '9') {
tree[i][0] = tree[i][0] * 10 + ch - '0';
ch = getchar();
}
}
else {
if (ch == '+') tree[i][0] = -1;
else if (ch == '-') tree[i][0] = -2;
else if (ch == '*') tree[i][0] = -3;
else if (ch == '/') tree[i][0] = -4;
scanf("%d %d", &tree[i][1], &tree[i][2]);
}
}
cout << "#" << test_case << " " << calc(1) << "\n";
}
return 0;
}