ํฐ์คํ ๋ฆฌ ๋ทฐ
๐ฉ๐ป๐ป ๋ฌธ์
โ๐ป ํ์ด
๐จ Go
// https://www.acmicpc.net/problem/15815
package main
import (
"bufio"
"fmt"
"os"
"strconv"
"strings"
)
func main() {
reader := bufio.NewReader(os.Stdin)
writer := bufio.NewWriter(os.Stdout)
defer writer.Flush()
var formula string
fmt.Fscanln(reader, &formula)
formulas := strings.Split(formula, "")
numbers := []int{}
for len(formulas) > 0 {
popped := formulas[0]
formulas = formulas[1:]
if popped == "+" || popped == "-" || popped == "*" || popped == "/" {
first := numbers[len(numbers)-2]
second := numbers[len(numbers)-1]
numbers = append(numbers[:len(numbers)-2], compute(popped, first, second))
} else {
number, _ := strconv.Atoi(popped)
numbers = append(numbers, number)
}
}
fmt.Fprintln(writer, numbers[0])
}
func compute(operator string, first, second int) int {
if operator == "+" {
return first + second
} else if operator == "-" {
return first - second
} else if operator == "*" {
return first * second
} else {
return first / second
}
}
๐จ Python3
# https://www.acmicpc.net/problem/15815
import sys
def compute(operator, first, second):
if operator == "+":
return first + second
elif operator == "-":
return first - second
elif operator == "*":
return first * second
else:
return first // second
if __name__ == "__main__":
formulas = list(sys.stdin.readline().rstrip())
numbers = []
while len(formulas) > 0:
popped = formulas.pop(0)
if popped == "+" or popped == "-" or popped == "*" or popped == "/":
second = numbers.pop(-1)
first = numbers.pop(-1)
numbers.append(compute(popped, first, second))
else:
numbers.append(int(popped))
print(numbers[0])
728x90
'dev > algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
BOJ / 3085๋ฒ / ์ฌํ ๊ฒ์ [Go][Python3] (0) | 2021.02.20 |
---|---|
BOJ / 1049๋ฒ / ๊ธฐํ์ค [Go][Python3] (0) | 2021.02.19 |
BOJ / 1802๋ฒ / ์ข ์ด ์ ๊ธฐ [Go][Python3] (0) | 2021.02.17 |
BOJ / 2428๋ฒ / ํ์ [Go][Python3] (0) | 2021.02.16 |
BOJ / 10546๋ฒ / ๋ฐฐ๋ถ๋ฅธ ๋ง๋ผํ ๋ [Go][Python3] (0) | 2021.02.15 |
๋๊ธ
๊ธ ๋ณด๊ดํจ
์ต๊ทผ์ ๋ฌ๋ฆฐ ๋๊ธ
TAG
- ๋ฐ๋
- baekjoon
- AWS
- ๋ฐฑ์ค
- go
- ์ด๋ถํ์
- Algorithm
- Golang
- ์๊ฐ๊ต์ฒด
- ์๊ณ ๋ฆฌ์ฆ
- ํ
- dp
- python3
- ๋ถํ ์ ๋ณต
- BFS
- ๋งฅ๋ถ
- Macbook pro 2012 mid 13
- java
- ํด์๋งต
- ๋งฅ๋ถ ์ ๊ทธ๋ ์ด๋
- ํ๋ก์ด๋์์ฌ
- ballet
- BOJ
- ๋ธ๋ฃจํธํฌ์ค
- MongoDB
- ๋งฅ๋ถํ๋ก
- ๋ชฝ๊ณ ๋๋น
- dfs
- ์๋ฐ
- ์คํ
- Total
- Today
- Yesterday