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
- java
- greedy
- 파이썬
- 코딩테스트
- Stack
- Class
- string
- SQL
- Binary Tree
- Method
- hash table
- array
- Counting
- implement
- Binary Search
- bit manipulation
- Data Structure
- Tree
- database
- dynamic programming
- geometry
- two pointers
- 코테
- simulation
- Number Theory
- 자바
- sorting
- Math
- 구현
- Matrix
Archives
- Today
- Total
코린이의 소소한 공부노트
[프로그래머스/Lv.0] 소인수분해 본문
1. Input
1) 자연수 n
2. Output
1) n의 소인수를 오름차순으로 담은 배열
3. Constraint
1) 2 <= n <= 10000
4. Example
Input: n=12 -> Output: {2,3}
설명: 12=2*2*3이므로 소인수는 2와 3
5. Code
1) 첫 코드(2022/10/27)
import java.util.ArrayList;
// main()
ArrayList<Integer> list = new ArrayList<Integer>();
// 1을 제외한 약수 찾기
for(int i=2 ; i<=n ; i++)
if(n%i==0)
list.add(i);
// 약수들 중 합성수 걸러내기
for(int i=list.size()-1 ; i>=0 ; i--){
int count=0;
for(int j=1 ; j<=list.get(i) ; j++){
if(list.get(i)%j==0)
count++;
if(count>=3){
list.remove(i);
break;
}
}
}
// 소인수 담기
int[] answer = new int[list.size()];
for(int i=0 ; i<answer.length ; i++)
answer[i] = list.get(i);
return answer;
'코딩테스트 풀이 > JAVA' 카테고리의 다른 글
[프로그래머스/Lv.0] 중복된 문자 제거 (0) | 2022.10.28 |
---|---|
[프로그래머스/Lv.0] 배열 원소의 길이 (0) | 2022.10.28 |
[프로그래머스/Lv.0] 숨어있는 숫자의 덧셈 (1) (0) | 2022.10.27 |
[프로그래머스/Lv.0] 문자열 정렬하기 (1) (0) | 2022.10.27 |
[프로그래머스/Lv.0] 모음 제거 (0) | 2022.10.27 |