[Programmers] 코딩테스트 입문 120848. 팩토리얼

이지현·2023년 2월 21일
0

Algorithm

목록 보기
30/81
post-thumbnail

✔️ Problem URL

팩토리얼


✔️ Problem

i 팩토리얼 (i!)은 1부터 i까지 정수의 곱을 의미합니다. 예를들어 5! = 5 4 3 2 1 = 120 입니다. 정수 n이 주어질 때 다음 조건을 만족하는 가장 큰 정수 i를 return 하도록 solution 함수를 완성해주세요.


✔️ Code

class Solution {
    public int solution(int n) {
        int answer = 0;
        
        for(int i = 1; i <= 10; i++) {
            if(factorial(i) <= n) {
                answer = i;
            }
            else {
                break;
            }
        }
        return answer;
    }
    
    public int factorial(int i) {
        if(i <= 1)
            return i;
        else
            return factorial(i-1) * i;
    }
}
profile
2023.09 ~ 티스토리 이전 / 2024.04 ~ 깃허브 블로그 이전

0개의 댓글