🖥️ CS/Baekjoon Algorithms
#1676번 팩토리얼 0의 개수 (C++)
한국의 메타몽
2020. 11. 9. 16:57
링크 : www.acmicpc.net/problem/1676
1676번: 팩토리얼 0의 개수
N!에서 뒤에서부터 처음 0이 아닌 숫자가 나올 때까지 0의 개수를 구하는 프로그램을 작성하시오.
www.acmicpc.net
문제풀이는 다음과 같다.
1. 5가 나올경우, 맨 뒤에 0이 추가된다.
2. 5의 제곱수가 나올경우, 제곱만큼 0이 추가된다 (ex: 5^2==25, 이 경우엔 0이 2개 추가됨)
3. 최대 범위가 500까지이니, 5의 3제곱 만큼 고려하면 된다.
#include <iostream>
#include <typeinfo>
using namespace std;
int main() {
ios::sync_with_stdio(NULL);
cin.tie(NULL);
int ans = 0, n = 0;
cin >> n;
for (int i = 5; i <= n; i *= 5) {
ans += n / i;
}
cout << ans << "\n";
return 0;
}