본문 바로가기

# Coding/# 백준

[백준 / 4811] 알약 - Python

728x90
반응형

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

 

4811번: 알약

입력은 최대 1000개의 테스트 케이스로 이루어져 있다. 각 테스트 케이스는 한 줄이며, 병에 들어있는 약의 개수 N ≤ 30 가 주어진다. 입력의 마지막 줄에는 0이 하나 주어진다.

www.acmicpc.net

<풀이>

해당 문제는 카탈란 수를 이용하여 풀었다.

공식 : Cn = (2*n)! / n! * (n+1)!

 

<전체 코드>

import math


def catalan(n):
    return math.factorial(2*n)//(math.factorial(n)*math.factorial(n+1))


while True:
    n = int(input())
    if n == 0:
        break
    print(catalan(n))
728x90
반응형