일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 소프트웨어공학
- programmers
- 그리디알고리즘
- 마라마라빔
- Bellman-Ford
- 함밥
- 백트래킹
- 장고
- Kruskal
- B대면노래방
- MyPlaylist
- DFS
- BFS
- 최소스패닝트리
- django
- 실습
- 모각코
- 알고리즘
- codetree
- 데이터베이스
- 프로그래머스
- 동적계획법
- DP
- Planned
- 종합설계
- 백준
- 파이썬
- minimum spanning tree
- SQL
- 코드트리
Archives
- Today
- Total
Leta Learns
[Python] 백준 1676번 - 팩토리얼 0의 개수 본문
문제 https://www.acmicpc.net/problem/1676
1676번: 팩토리얼 0의 개수
N!에서 뒤에서부터 처음 0이 아닌 숫자가 나올 때까지 0의 개수를 구하는 프로그램을 작성하시오.
www.acmicpc.net

import sys
input = sys.stdin.readline
def factorial(n):
fac = 1
for i in range(1, n+1):
fac *= i
return fac
n = int(input())
ans = factorial(n)
strans = str(ans)
cnt = 0
for i in range(len(strans)-1, -1, -1):
if strans[i] == '0':
cnt += 1
else:
break
print(cnt)
#2022-08-06 (코드 수정)
모각코 글에 어떤 분이 math 모듈에 factorial함수가 있다고 알려주셔서 코드를 수정했다! 다시 한 번 감사합니다.
#math 모듈 factorial 함수 사용한 코드
import sys, math
input = sys.stdin.readline
n = int(input())
ans = math.factorial(n)
strans = str(ans)
cnt = 0
for i in range(len(strans)-1, -1, -1):
if strans[i] == '0':
cnt += 1
else:
break
print(cnt)

'Coding > 백준' 카테고리의 다른 글
[Python] 백준 9375번 - 패션왕 신해빈 (0) | 2022.08.06 |
---|---|
[Python] 백준 1541번 - 잃어버린 괄호 (0) | 2022.08.03 |
[Python] 백준 1654번 - 랜선 자르기 (0) | 2022.07.27 |
[Python] 백준 1436번 - 영화감독 숌 (0) | 2022.07.23 |
[Python] 백준 1463번 - 1로 만들기 (0) | 2022.07.23 |
Comments