반응형
백준 1676번 문제 - 팩토리얼 0의 개수
1676번: 팩토리얼 0의 개수
N!에서 뒤에서부터 처음 0이 아닌 숫자가 나올 때까지 0의 개수를 구하는 프로그램을 작성하시오.
www.acmicpc.net
문제 분석
- 팩토리얼 계산을 했을 때 뒤에서부터 처음 0이 아닌 숫자가 나올 때까지 0의 개수를 출력해라
해결 키 포인트
- 숫자가 int, long보다 커지기 때문에 BigInteger 사용
- BigInteger.valueOf(1); : BigInteger 숫자 설정
- bigInteger.multiply(n); : bigInteger의 n을 곱한다.
코드
import java.io.*;
import java.math.BigInteger;
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());
BigInteger factorial = BigInteger.valueOf(1);
for (int i = 2; i <= N; i++) {
factorial = factorial.multiply(BigInteger.valueOf(i));
}
String s = String.valueOf(factorial);
int count = 0;
for (int i = s.length()-1; i >= 0; i--) {
if(s.charAt(i) != '0')
break;
count++;
}
System.out.println(count);
}
}
반응형
'CodingTEST' 카테고리의 다른 글
[백준 4949] 균형잡힌 세상 (JAVA) (0) | 2023.12.05 |
---|---|
[백준 11651] 좌표 정렬하기 2 (JAVA) (1) | 2023.12.05 |
[백준 1436] 영화감독 숌(JAVA) (0) | 2023.12.05 |
[백준 1018] 체스판 다시 칠하기(JAVA) (0) | 2023.11.30 |
[백준 10866] 덱 (JAVA) (0) | 2023.11.30 |