Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- bit manipulation
- 코딩테스트
- Number Theory
- Data Structure
- Math
- 구현
- dynamic programming
- 코테
- Tree
- string
- sorting
- Binary Tree
- two pointers
- hash table
- 자바
- Method
- Binary Search
- geometry
- 파이썬
- implement
- java
- greedy
- array
- simulation
- Matrix
- database
- SQL
- Stack
- Counting
- Class
Archives
- Today
- Total
코린이의 소소한 공부노트
[프로그래머스/Lv.1] 나머지가 1이 되는 수 찾기 본문
1. Input
1) 자연수 n
2. Output
1) 자연수 x
2) n을 x로 나눈 나머지가 1이 되는 가장 작은 자연수가 x
3. Constraint
1) 3 ≤ n ≤ 1,000,000
4. Example
Input: n=10 -> Output: 3
설명:
10 % 1 = 0
10 % 2 = 0
10 % 3 = 1
5. Code
1) 첫 코드(2022/??)
int answer = 0;
for(int i=2 ; i<n ; i++){
if(n%i == 1){
answer = i;
break;
}
}
return answer;
2) 조금 더 개선해본 코드(2022/11/09)
for(int i=2 ; i<n-1 ; i++) // i=n-2까지 해보고 그래도 없다면 n-1 반환
if(n%i==1)
return i;
return n-1;
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[프로그래머스/Lv.1] 없는 숫자 더하기 (0) | 2022.11.11 |
---|---|
[프로그래머스/Lv.1] 최소직사각형 (0) | 2022.11.09 |
[프로그래머스/Lv.0] A로 B 만들기 (0) | 2022.11.09 |
[프로그래머스/Lv.0] 이진수 더하기 (0) | 2022.11.09 |
[프로그래머스/Lv.0] 문자열 밀기 (0) | 2022.11.09 |