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
- BOJ
- BFS
- 시뮬레이션
- 문제풀이
- 문제해결
- dfs
- 백준
- math
- 코테
- MySQL
- 러닝일지
- 투포인터
- db
- priorityqueue
- SWEA
- binary search
- COS PRO
- 자바
- greedy
- 이분탐색
- oracle
- 알고리즘
- 코딩테스트
- 문자열
- Java
- DP
- 운동기록
- 프로그래머스
- SQL
- 건강
Archives
- Today
- Total
슈콩
[프로그래머스] Lv.2 소수 찾기 본문

[문제]
프로그래머스
SW개발자를 위한 평가, 교육의 Total Solution을 제공하는 개발자 성장을 위한 베이스캠프
programmers.co.kr
[소스 코드]
import java.util.*;
class Solution {
static boolean[] composite = new boolean[10000000];
static boolean[] visit;
static String input,s;
static int n,answer;
static Set<Integer> hs = new HashSet<>();
public int solution(String numbers) {
input = numbers;
composite[0] = composite[1] = true;
for(int i=2;i*i<=9999999;i++){
for(int j=i*i;j<=9999999;j+=i){
composite[j] = true;
}
}
answer = 0;
n = numbers.length();
for(int i=1;i<=n;i++) {
visit = new boolean[n];
s = "";
find(0,i,s);
}
return answer;
}
private static void find(int cnt,int maxCnt,String s){
if(cnt==maxCnt){
int num = Integer.valueOf(s);
if(!composite[num] && hs.add(num)){
answer++;
}
return;
}
for(int i=0;i<n;i++){
if(!visit[i]){
visit[i] = true;
find(cnt+1,maxCnt,s+input.charAt(i));
visit[i] = false;
}
}
}
}'Algorithms > Programmers' 카테고리의 다른 글
| [프로그래머스] Lv.1 체육복 (0) | 2025.10.09 |
|---|---|
| [프로그래머스] Lv.1 모의 고사 (0) | 2025.10.09 |
| [프로그래머스] Lv.2 가장 큰 수 (0) | 2025.10.08 |
| [프로그래머스] Lv.1 K번째수 (0) | 2025.10.08 |
| [프로그래머스] Lv.2 게임 맵 최단 거리 (0) | 2025.10.08 |