본문 바로가기

프로그래머스/lv1

나머지가 1이 되는 수 찾기

/* 나머지가 1이 되는 수 찾기
 * 자연수 n이 매개변수로 주어집니다.
 * n을 x로 나눈 나머지가 1이 되도록 하는
 * 가장 작은 자연수 x를 return 하도록 solution 함수를 완성해주세요.
 * 답이 항상 존재함은 증명될 수 있습니다.
 *
 * 3 ≤ n ≤ 1,000,000
 *
 * n    result
 * 10   3
 * 12   11
 */
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class programmer_1_18 {
    static int a1 = 10;
    static int a2 = 12;
    public int solution(int n) {
        int answer = 0;
        for(int i = 1; i <= n; i++){
            if(n % i == 1){
                answer = i;
                break;
            }
        }
        return answer;
    }
    public static void main(String args[]){
        programmer_1_18 t = new programmer_1_18();
        System.out.println("---------------------------------------");
        System.out.println("result = " + t.solution(a1));
        System.out.println("---------------------------------------");
        System.out.println("result2 = " + t.solution(a2));
        System.out.println("---------------------------------------");
    }
}
cs

'프로그래머스 > lv1' 카테고리의 다른 글

콜라츠 추측  (0) 2022.12.13
두 정수 사이의 합  (1) 2022.12.13
정수 내림차순으로 배치하기  (0) 2022.12.13
하샤드 수  (0) 2022.12.13
x만큼 간격이 있는 n개의 숫자  (0) 2022.12.13