[BOJ/1676번] 팩토리얼 0의 개수

sky·2022년 11월 1일
0

BaekJoon Online Judge(S)

목록 보기
20/21
post-thumbnail

문제

Silver Ⅴ

N!에서 뒤에서부터 처음 0이 아닌 숫자가 나올 때까지 0의 개수를 구하는 프로그램을 작성하시오.

입력
첫째 줄에 N이 주어진다. (0 ≤ N ≤ 500)

출력
첫째 줄에 구한 0의 개수를 출력한다.


Solution

Python

import math
n = int(input())
n = list(str(math.factorial(n)))
cnt = 0
for i in n[::-1]:
    if i == "0":
        cnt += 1
    else:
        break
print(cnt)

실패한 풀이

Java

import java.util.*;
public class Main
{
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    int n = sc.nextInt();
		n = factorial(n);
		//String str = Integer.toString(n);
		String[] num = new String[n.length()];
		
		System.out.println(Arrays.toString(n));
		
		int cnt = 0;
		for(int i=str.length()-1;i>=0;i--){
		    if (num[i] == "0") cnt++;
		    else break;
		}
		System.out.println(cnt);
	}
	
	public static int factorial (int num) {
        if (num == 1)
            return 1;
        
        return num * factorial ( num - 1 );
    }
}

오랜만에 자바에 손을 댔더니 난항을 겪는 중..


Total Time

  • 2022-11-01 | 19:55 - 20:00 Success!
profile
개발자가 되고 싶은 1人

0개의 댓글