-
Notifications
You must be signed in to change notification settings - Fork 0
/
boj1935.go
52 lines (46 loc) · 1019 Bytes
/
boj1935.go
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
package main
import (
"bufio"
"fmt"
"os"
)
func solvePostfix(expr string, mapper map[rune]float64) float64 {
var stack []float64
for _, e := range expr {
if 'A' <= e && e <= 'Z' {
stack = append(stack, mapper[e])
} else {
num2 := stack[len(stack)-1]
num1 := stack[len(stack)-2]
stack = stack[:len(stack)-2]
if e == '+' {
stack = append(stack, num1+num2)
} else if e == '-' {
stack = append(stack, num1-num2)
} else if e == '*' {
stack = append(stack, num1*num2)
} else if e == '/' {
stack = append(stack, num1/num2)
}
}
}
return stack[0]
}
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var n int
fmt.Fscanln(reader, &n)
var expr string
fmt.Fscanln(reader, &expr)
mapper := make(map[rune]float64)
var num float64
for i := 0; i < n; i++ {
fmt.Fscanln(reader, &num)
mapper[rune('A'+i)] = num
}
fmt.Println(mapper)
result := solvePostfix(expr, mapper)
fmt.Fprint(writer, result)
}