프로그래머스/lv1
나머지가 1이 되는 수 찾기
do_it0904
2022. 12. 13. 08:25
/* 나머지가 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 |