[BOJ] 1676번 팩토리얼 0의개수

호호빵·2022년 10월 19일
0

Algorithm

목록 보기
36/46

문제

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

풀이

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(br.readLine());
        int answer = 0;

        while (N >= 5){
            answer += N / 5;
            N = N / 5;
        }

        System.out.println(answer);
    }
}
1. 연속적인 0의 개수를 뒤에서부터 구하려면 곱해진 10의 개수를 알아야한다.
2. 2x5의 개수를 알야하는데 5가 더 적게 나오는 소인수이니 5의 개수가 곧 10의 개수이다.
   -> N까지 곱해진 5의 개수를 세면 되는 문제!
   -> 25, 125, 625 등 5의 제곱수인 경우 제곱 횟수만큼 더 count
   
* 100일 경우
  100/5  + 100/(5*5)
  (20)		(4) <-  25, 50, 75, 100  	
  
* 위 과정을 while문으로 풀어내는 과정이 어려웠다.   


팩토리얼 0의개수 풀이

profile
하루에 한 개념씩

0개의 댓글