문제
i팩토리얼 (i!)은 1부터 i까지 정수의 곱을 의미합니다. 예를들어 5! = 5 * 4 * 3 * 2 * 1 = 120 입니다. 정수 n이 주어질 때 다음 조건을 만족하는 가장 큰 정수 i를 return 하도록 solution 함수를 완성해주세요.
i! ≤ n
솔루션
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
int solution(int n) {
int answer = 0;
int mul = 1;
if(n == 1)
return 1;
for(int i = 1; i <= n; i++){
mul *= i;
if(mul == n)
return i;
if(mul > n){
answer = i - 1;
break;
}
}
return answer;
}
https://school.programmers.co.kr/learn/courses/30/lessons/120848
'프로그래머스코딩테스트연습풀이 > C언어' 카테고리의 다른 글
[프로그래머스/C언어] 개미 군단 (0) | 2023.08.03 |
---|---|
[프로그래머스/C언어] 주사위의 개수 (0) | 2023.08.03 |
[프로그래머스/C언어] 숨어있는 숫자의 덧셈 (1) (0) | 2023.08.03 |
[프로그래머스/C언어] 최댓값 만들기 (2) (0) | 2023.08.03 |
[프로그래머스/C언어] 삼각형의 완성조건 (1) (0) | 2023.08.03 |