문제

N!에서 뒤에서부터 처음 0이 아닌 숫자가 나올 때까지 0의 개수를 구하는 프로그램을 작성하시오.

입력

첫째 줄에 N이 주어진다. (0 ≤ N ≤ 500)

출력

첫째 줄에 구한 0의 개수를 출력한다.

예제 입력 1

10

예제 출력 1

2

더보기

Solution

#include<stdio.h>

int divideby2(int N)
{
	int count=0;

	while(N%2==0)
	{
		count++;
		N/=2;
	}

	return count;
}

int divideby5(int N)
{
	int count=0;

	while(N%5==0)
	{
		count++;
		N/=5;
	}

	return count;
}

int main(void)
{
	int N, two=0, five=0;

	scanf("%d", &N);

	for(int i=2;i<=N;i++)
	{
		two+=divideby2(i);
		five+=divideby5(i);
	}

	printf("%d\n", two<five?two:five);

	return 0;
}
728x90

+ Recent posts