
나의 풀이
class Solution {
public int solution(int n) {
int answer = 0;
for(int x = 1 ; x < n; x++){
if(n % x == 1){
answer = x;
break;
}
}
return answer;
}
}
n을 1로 나누면 나머지는 항상 0이므로 i를 2로 초기화 하는 것이 적절한 것 같다..
다른 사람의 풀이
class Solution {
public int solution(int n) {
int answer = 2;
while(true){
if(n % answer == 1){
break;
}
answer++;
}
return answer;
}
}
'알고리즘' 카테고리의 다른 글
| [프로그래머스] 자연수 뒤집어 배열로 만들기(Java) * (0) | 2024.11.01 |
|---|---|
| [프로그래머스] x만큼 간격이 있는 n개의 숫자(Java) (0) | 2024.10.31 |
| [프로그래머스] 약수의 합(Java) (0) | 2024.10.29 |
| [프로그래머스] 평균 구하기(Java) (0) | 2024.10.28 |
| [프로그래머스] 자릿수 더하기(Java) * (0) | 2024.10.28 |