반응형

Prob

 


Approach

itertools를 사용한다.

음수 // 양수에 주의하여 작성한다.


Code

from itertools import permutations
import sys
def cal_formul(case, num):
    res = num[0]
    for i in range(len(num) - 1):
        if case[i] == '+':
            res += num[i + 1]
        elif case[i] == '-':
            res -= num[i + 1]
        elif case[i] == '*':
            res *= num[i + 1]
        elif case[i] == '/':
            if res < 0:
                res = -(abs(res) // num[i + 1])
            else:
                res //= num[i + 1]
    return res

if __name__ == "__main__":
    N = int(input())
    num = list(map(int, input().split()))
    oper = list(map(int, input().split()))
    m_oper = ""

    for i in range(len(oper)):
        if oper[i] > 0 and i == 0:
            m_oper += "+" * oper[i]
        elif oper[i] > 0 and i == 1:
            m_oper += "-" * oper[i]
        elif oper[i] > 0 and i == 2:
            m_oper += "*" * oper[i]
        elif oper[i] > 0 and i == 3:
            m_oper += "/" * oper[i]
    m_oper = list(set((permutations(m_oper, len(m_oper)))))
    min = sys.maxsize
    max = -sys.maxsize - 1
    
    for case in m_oper:
        val = cal_formul(case, num)
        if val < min : min = val
        if val > max : max = val

    print(max)
    print(min)

 

출처

https://www.acmicpc.net/problem/14888

반응형

'프로젝트 > Baekjoon' 카테고리의 다른 글

BAEKJOON PYTHON 2580 스도쿠  (0) 2021.09.02
BAEKJOON PYTHON 9663 N-Queen  (0) 2021.09.02
BAEKJOON PYTHON 15652 N과 M (4)  (0) 2021.09.02
BAEKJOON PYTHON 15651 N과 M (3)  (0) 2021.09.02
BAEKJOON PYTHON 15650 N과 M (2)  (0) 2021.08.31
블로그 이미지

Refrin

일상생활 끄적 IT 프로젝트 끄적