반응형
문제
(nm) 0 의 개수를 출력하는 프로그램을 작성하시오.
의 끝자리입력
첫째 줄에 정수 n , m (0≤m≤n≤2,000,000,000 , n≠0 )이 들어온다.
출력
첫째 줄에 (nm) 의 끝자리 0 의 개수를 출력한다.
예제 입력 1
25 12
예제 출력 1
2
알고리즘 분류
수학(math), 정수론(number_theory)
소스코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer in = new StringTokenizer(br.readLine());
int N = Integer.parseInt(in.nextToken());
int M = Integer.parseInt(in.nextToken());
int cnt2 = count2(N) - count2(M) - count2(N - M);
int cnt5 = count5(N) - count5(M) - count5(N - M);
System.out.println(Math.min(cnt2, cnt5));
}
public static int count5(int num) {
int count = 0;
while (num >= 5) {
count += (num / 5);
num /= 5;
}
return count;
}
public static int count2(int num) {
int count = 0;
while (num >= 2) {
count += (num / 2);
num /= 2;
}
return count;
}
}
반응형
'코딩테스트 > 백준' 카테고리의 다른 글
JAVA 백준 15650번 N과 M (2) (0) | 2022.07.14 |
---|---|
JAVA 백준 15649번 N과 M (1) (0) | 2022.07.13 |
JAVA 백준 1676번 팩토리얼 0의 개수 (0) | 2022.07.12 |
JAVA 백준 9375번 패션왕 신해빈 (0) | 2022.07.12 |
JAVA 백준 1010번 다리 놓기 (0) | 2022.07.11 |
댓글