/* 팩토리얼
* i팩토리얼 (i!)은 1부터 i까지 정수의 곱을 의미합니다.
* 정수 n이 주어질 때 다음 조건을 만족하는 가장 큰 정수 i를 return
* i! ≤ n
* 0 < n ≤ 3,628,800
*
* n result
* 3628800 10
* 10! = 3,628,800입니다. n이 3628800이므로 최대 팩토리얼인 10을 return
* 7 3
* 3! = 6, 4! = 24입니다. n이 7이므로, 7 이하의 최대 팩토리얼인 3을 return
*/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | public class programmer_0_81 { static int a1 = 3628800; static int a2 = 7; public int solution(int n) { int answer = 0; int val = 1; for(int i = 1; i <= n; i++){ val *= i; if(val == n){ answer = i; break; } else if(val > n){ answer = i-1; break; } } return answer; } public static void main(String args[]){ programmer_0_81 t = new programmer_0_81(); // System.out.println("---------------------------------------"); // System.out.println("result = " + t.solution(a1)); System.out.println("---------------------------------------"); System.out.println("result2 = " + t.solution(a2)); System.out.println("---------------------------------------"); } } | cs |