Notice
Recent Posts
Recent Comments
Link
«   2024/07   »
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 29 30 31
Archives
Today
Total
관리 메뉴

KSI일기장

프로그래머스 팩토리얼 (자바) (for문) 본문

JavaAlgorithm

프로그래머스 팩토리얼 (자바) (for문)

MyDiaryYo 2024. 1. 19. 11:28

 

 

 

 

 

1.

class Solution {
    public int solution(int n) {
        int answer = 0;
        int a = 1;

        for (int i=1; a <= n; i++){	//i는 a가 n이상이 되기전까지 (미만일때까지) 계속 증가
            answer = i;
            a  *= i;
        }
        return answer-1;
    }
}

 

 

2.

class Solution {
    public int solution(int n) {
        int answer = 0;
        int a = 1;

       for (int i=1; i <= n; i++){
            if(a<=n){
                a  *= i;
                answer = i;
            }
        }
        return answer-1;
    }
}

 

 

1번과 2번은 같은 방법 for문안 조건 차이