백준
2553번: 마지막 팩토리얼 수
bingual
2022. 2. 21. 10:58
반응형
풀이
방법은 두가지가 있습니다.
1. 산술적으로 풀이하는 방법.
2. 단순히 팩토리얼 값을 문자열로 변환하여 리버스한뒤 체크하며 풀이하는 방법.
자바는 오버플로우를 방지하기 위해서 result 값에 나머지연산을 한뒤 진행합니다.
자바
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
long n = Integer.parseInt(br.readLine());
long result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
result %= 10000000;
while (result % 10 == 0)
result /= 10;
}
System.out.print(result % 10);
}
}
파이썬
def fac(n):
result = 1
for i in range(1, n + 1):
result *= i
return result
n = int(input())
check = str(fac(n))
check = check[::-1]
for i in check:
if i != '0':
print(i)
break;