본문 바로가기
알고리즘/백준 BOJ

[ BOJ/C# ] 1676 팩토리얼 0의 개수

by 왹져박사 2023. 9. 3.

https://www.acmicpc.net/problem/1676

 

1676번: 팩토리얼 0의 개수

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

www.acmicpc.net

N!에서 뒤에서부터 처음 0이 아닌 숫자가 나올 때까지 0의 개수를 구하려면, 

결국 10이 되는 숫자인 2와 5가 중요하다.

따라서 2와 5중 최솟값이 0의 개수가 된다. 

using System;
using System.Runtime.Intrinsics.Arm;

namespace _1676
{
    class Program
    {
        static void Main()
        {
            StreamReader sr = new StreamReader(Console.OpenStandardInput());
            StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());

            int n = int.Parse(sr.ReadLine());
            int count5 = 0;
            int count2 = 0;

            for(int i = 2; i <= n; i++)
            {
                int m = i;
                while (m % 5 == 0)
                {
                    count5++;
                    m /= 5;
                }
                while (m % 2 == 0)
                {
                    count2++;
                    m /= 2;
                }
            }

            if (count5 > count2) sw.WriteLine(count2);
            else sw.WriteLine(count5);

            sr.Close();
            sw.Flush();
            sw.Close();
        }
    }
}

'알고리즘 > 백준 BOJ' 카테고리의 다른 글

[ BOJ/C# ] 2164 카드2  (0) 2023.09.05
[ BOJ/C# ] 1920 수 찾기  (0) 2023.09.03
[ BOJ/C# ] 1546 평균  (0) 2023.09.01
[ BOJ/C# ] 2869 달팽이는 올라가고 싶다  (0) 2023.09.01
[ BOJ/C# ] 10989 수 정렬하기 3  (0) 2023.08.30