Algorithm
[수학 3 단계] 백준 1676번 팩토리얼 0의 개수
이무쿤
2020. 9. 29. 16:34
반응형
문제
N!에서 뒤에서부터 처음 0이 아닌 숫자가 나올 때까지 0의 개수를 구하는 프로그램을 작성하시오.
입력
첫째 줄에 N이 주어진다. (0 ≤ N ≤ 500)
출력
첫째 줄에 구한 0의 개수를 출력한다.
1,2,3,4,....., N에서 2가 몇번 곱해지는가, 5가 몇번 곱해지는가 확인
2와 5의 곱으로 10이 만들어지므로 이 둘중 작은 것 만큼 10이 만들어진다.
#include <iostream>
#include <vector>
#include<queue>
#include <string>
#include<cmath>
#include<string.h>
#include <map>
#include<algorithm>
using namespace std;
map<string, int> m;
vector<int> vec;
long long factorial(long long n)
{
return (n == 1 || n == 0) ? 1 : factorial(n - 1) * n;
}
int main() {
long long N;
cin >> N;
int two = 0;
int five = 0;
for (int i = 1; i <= N; i++)
{
if (i % 2 == 0)
{
int num = i;
while (1)
{
if (num % 2 == 0)
{
num /= 2;
two++;
}
else
{
break;
}
}
}
if (i % 5 == 0)
{
int num = i;
while (1)
{
if (num % 5 == 0)
{
num /= 5;
five++;
}
else
{
break;
}
}
}
}
cout << min(two, five) << endl;
}
반응형