https://www.acmicpc.net/problem/1676
1676번: 팩토리얼 0의 개수
N!에서 뒤에서부터 처음 0이 아닌 숫자가 나올 때까지 0의 개수를 구하는 프로그램을 작성하시오.
www.acmicpc.net
자 이번엔 쉬운 문제다
문제를 보면 해당 팩토리얼을 했을 때 뒤에서부터 0이 아닌 갯수를 찾는 것이다.
2의 갯수, 5의 갯수, 10의 갯수를 카운트해서
그 수만큼 올리면 된다.
끝
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
// TODO Auto-generated method stub
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int N=Integer.parseInt(br.readLine());
int two = 0;
int five = 0;
int ten = 0;
for(int i=1;i<=N;i++) {
int init=i;
while(init%10==0) {
init/=10;
ten++;
}
while(init%5==0) {
init/=5;
five++;
}
while(init%2==0) {
init/=2;
two++;
}
}
int answer= two >= five ? five+ten : two+ten;
System.out.println(answer);
}
}
반응형
'Algorithm > Baekjoon For.Java' 카테고리의 다른 글
1620 : 나는야 포켓몬 마스터 이다솜 (0) | 2023.03.24 |
---|---|
11723 : 집합 (0) | 2023.03.22 |
2292 : 벌집 (0) | 2023.03.20 |
1354 : 무한 수열 2 (0) | 2023.03.20 |
10816 : 숫자 카드 2 (0) | 2023.03.17 |