[백준/Python3] 1676 팩토리얼 0의 개수

nyam·2022년 3월 10일
0

백준

목록 보기
12/34
post-thumbnail

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


풀이

N! 값을 구하고 뒤에서 부터 0이 아닌 숫자가 나올 때 까지 0의 개수를 구하는 문제다. 이 문제는 다음과 같은 순서로 해결했다.

  1. 팩토리얼 값 구하기
  2. 뒤에서부터 0의 개수를 구함

팩토리얼은 재귀 함수를 통해 구할 수 있다. 0의 개수는 N!을 거꾸로 뒤집어 참조해 구했다.

코드

def get_factorial(N: int) -> int:
    if N == 0:
        return 1
    else:
        return N * get_factorial(N-1)

N = int(input())
ret = 0
fact = list(reversed(list(str(get_factorial(N)))))

for i in range(0, len(fact)):
    if fact[i] == '0':
        ret = ret + 1
    else:
        break

print(ret)

0개의 댓글