[프로그래머스/C++]Lv.0 - 팩토리얼

YH J·2023년 4월 19일
0

프로그래머스

목록 보기
46/168

문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/120848

내 풀이

1씩 더한 값을 곱해가면서 n을 넘는지 체크한다.

내 코드

#include <string>
#include <vector>

using namespace std;

int solution(int n) {
    
    int answer = 1;
    int a = 1;
    
    while(1)
    {
        ++answer;
        if(a * answer > n)
        {
            answer--;
            return answer;
        }
        else
            a *= answer;
    }
    
    return answer;
}

다른 사람의 풀이

#include <string>
#include <vector>

using namespace std;
int factorial(int n)
{
    if(n == 0 || n == 1)
        return 1;
    else
        return n*factorial(n-1);
}
int solution(int n) {
    int i = 0;
    while(factorial(i) <= n)
        i++;
    i--;
    return i;
}

다른 사람의 풀이 해석

팩토리얼을 재귀함수로 구현해놓고 사용하였다.

profile
게임 개발자 지망생

0개의 댓글