[백준1676] 팩토리얼 0의 개수 (C++)

유후·2022년 3월 20일
0

백준

목록 보기
9/66

BOJ 바로가기

#include <iostream>
using namespace std;
int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	int n, count = 0;
	cin >> n;
	while (1) {
		int k = n;
		n--;
		if (k == 0)
			break;
		while (1) {
			if (k % 5 == 0) {
				k /= 5;
				count++;
			}
			else
				break;
		}
	}
	cout << count;
	return 0;
}

팩토리얼의 경우 소인수분해를 했을 때 5가 2보다 훨씬 적기 때문에 수를 5로 나누었을 때 나누어지는 횟수를 변수 count에 차근차근 더해주면 답을 구할 수 있다.

profile
이것저것 공부하는 대학생

0개의 댓글