※참고
n=0
0! = 1
aws = 0
print = 1
n의 범위가 <= 500 이기 때문에 숫자가 매우 커서 int, long으로 담기지 않는다. 그래서 문자열로 저장하는 BigInteger을 사용해서 숫자를 담아줘야한다.
문제를 이해하기만 하고 BigInteger의 사용법만 숙지하면 쉽게 풀 수 있는 문제인듯 하다.
import java.io.*;
import java.math.BigInteger;
public class Main {
static BigInteger factorial(int n){
BigInteger fact = new BigInteger("1");
BigInteger temp = new BigInteger(String.valueOf(n));
if(n == 0){
return BigInteger.valueOf(1);
}
while(n-- > 1){
fact = fact.multiply(temp);
temp = temp.subtract(BigInteger.ONE);
}
return fact;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String fact = String.valueOf(factorial(n));
int zero_count = 0;
for (int i = fact.length()-1; i >= 0; i--) {
if(fact.charAt(i)=='0'){
zero_count++;
} else{
break;
}
}
System.out.print(zero_count);
}
}
'CodingTest' 카테고리의 다른 글
[Coding Test] 백준 1929번 (1) | 2024.04.19 |
---|---|
[CodingTest] 백준 1920번 (0) | 2024.04.17 |
[CodingTest] 백준 1874번 (0) | 2024.04.17 |
[CodingTest] 백준 10989번 (0) | 2024.04.16 |
[CodingTest] 백준 9012번 (0) | 2024.04.16 |