sw사관학교 정글 2기/01 기초,재귀,완전탐색, 정렬
[재귀함수] 백준 10869번 팩토리얼 with Python3
금의야행
2021. 8. 10. 08:53
https://www.acmicpc.net/problem/10872
10872번: 팩토리얼
0보다 크거나 같은 정수 N이 주어진다. 이때, N!을 출력하는 프로그램을 작성하시오.
www.acmicpc.net
문제
내 풀이
def factorial(n: int):
if n > 0:
return n * factorial(n-1)
else:
return 1
n = int(input())
print(factorial(n))
해설
재귀를 쓰고 싶었으나, 제대로 이해하지 못하고 단순한 for문의 대체로만 사용했다.
정답 풀이
def factorial(n):
result = 1
if n > 0 :
result = n * factorial(n-1)
return result
n = int(input())
print(factorial(n))
n = int(input())
result = 1
if n > 0:
for i in range(1, n+1):
result *= i
print(result)
출처:https://ooyoung.tistory.com/114
해석