알고리즘/구현,시뮬
BOJ - 1676 ) 팩토리얼 0의 개수
개발자가될수있을까?
2022. 3. 3. 22:03


https://www.acmicpc.net/problem/1676
1676번: 팩토리얼 0의 개수
N!에서 뒤에서부터 처음 0이 아닌 숫자가 나올 때까지 0의 개수를 구하는 프로그램을 작성하시오.
www.acmicpc.net
0의 개수는 1~N까지의 2와 5의 개수중 작은것만큼 생기므로
N까지 탐색하며 개수를 새준다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static int a,b;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
for(int i=1;i<=n;i++) {
divide(i);
}
System.out.println(a>b?b:a);
}
private static void divide(int i) {
if(i%2==0&&i%5==0) {
a++;b++;
divide(i/10);
} else if(i%5==0) {
b++;
divide(i/5);
} else if(i%2==0) {
a++;
divide(i/2);
}
return;
}
}
