풀스택 개발자 2020. 9. 2. 16:05

이 방법은

int형이 한계치에 도달하면 동작이 안됨... 오버플로우...

#include<iostream>
#include<vector>

using namespace std;
int main() {
	int tmp = 0;
	int max = 0;
	int cnt = 0;
	int result = 1;
	int N = 0;
	cin >> N;
	for (int i = 1; i <= N; i++)
	{
		result = result * i;
	}
	
	while (1)
	{
		
		tmp = result;
		tmp = tmp % 10;
		result = result / 10;
		if (result == 0)
		{
			break;
		}
		if (tmp == 0)
		{
			cnt++;
		}
		else if (tmp != 0)
		{
			cnt = 0;
		}
		if (max < cnt)
		{
			max = cnt;
		}
	}
	cout << max;
}

해결방안

 

모든 수를 곱해서 10으로 나눠 각 자리숫자를 구해 비교하지말고

각 숫자의 소인수분해를 해서 2와 5의 쌍의 갯수를 구하면 해답이 풀린다...

 

#include<iostream>
#include<vector>

using namespace std;
int main() {
	int tmp = 0;
	int j = 0;
	int N = 0;
	int result = 1;
	cin >> N;
	int t = 0;
	int f = 0;
	for (int i = 2; i <= N; i++)
	{
		j = 2;
		tmp = i;
		while (1)
		{
			if (tmp%j == 0)
			{	
				if (j == 2)
				{
					t++;
				}
				else if (j == 5)
				{
					f++;
				}
				tmp = tmp / j;
			}
			else j++;
			if (tmp == 1)
			{
				break;
			}
		}
	}
	if (t > f)
	{
		cout << f;
	}
	else
		cout << t;
	
}