프로그래머스 - LV1. 나머지가 1이 되는 수 찾기

김소정·2022년 3월 2일
0

프로그래머스

목록 보기
31/35

❔ 문제

❗ 내 풀이

class Solution {
    public int solution(int n) {
        int answer = 0;
        
        for(int i = 1; i <= n; i++){
            if(n % i == 1){
                answer = i;
                break;
            }
        }
        
        return answer;
    }
}

🚩참고 (다른 풀이)

1. 
class Solution {
    public int solution(int n) {
                int answer = 0;
        int x =2;
        while(true) {
            if(n%x==1) {
                answer = x;
                break;
            }
            x++;
        }
        return answer;
    }
}

📝 정리

💬 익숙치 않은 while문 잘 활용하기

✔ while(true)


profile
개발자 가보자고

0개의 댓글

관련 채용 정보