[백준] 20164번 홀수 홀릭 호석

HL·2021년 6월 24일
0

백준

목록 보기
98/104
post-custom-banner

문제링크

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

코드

import sys


def solution():
    read = sys.stdin.readline
    num = read().rstrip()
    print(rec(num, 1), rec(num, 0))


def rec(num, is_min):

    count = get_count(num)

    if len(num) == 1:
        return count

    elif len(num) == 2:
        num = int(num[0]) + int(num[1])
        return count + rec(str(num), is_min)

    else:
        result = float('-inf')
        if is_min:
            result = float('inf')
        for s1 in range(1, len(num)-1):
            for s2 in range(s1+1, len(num)):
                num1 = int(num[:s1])
                num2 = int(num[s1:s2])
                num3 = int(num[s2:len(num)])
                if is_min:
                    result = min(result, rec(str(num1 + num2 + num3), is_min))
                else:
                    result = max(result, rec(str(num1 + num2 + num3), is_min))
        return count + result


def get_count(num):
    count = 0
    for i in range(len(num)):
        if int(num[i]) % 2 == 1:
            count += 1
    return count


solution()
profile
Frontend 개발자입니다.
post-custom-banner

0개의 댓글