코딩테스트/백준

[Java] 백준 1676번 : 팩토리얼 0의 개수

Eungae-D 2024. 4. 15. 23:15
728x90

문제

 

1676번: 팩토리얼 0의 개수

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

www.acmicpc.net

 


 

코드

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {
    private static StringBuilder sb;
    private static BufferedReader br;
    private static StringTokenizer st;
    private static int N;
    private static int answer = 0;

    //입력
    public static void input() throws Exception {
        br = new BufferedReader(new InputStreamReader(System.in));
        sb = new StringBuilder();

        N = Integer.parseInt(br.readLine());
    }

    //실행
    public static void process() {
        //n! 에서 뒤에 0 이 나오는 조건은 2*5가 곱해져 있을때다. 즉 5의 갯수를 세주면 된다.
        while(N>=5){
            answer += N/5;
            N /= 5;
        }
        System.out.println(answer);
    }


    public static void main(String[] args) throws Exception {
        input();
        process();
    }
}