File tree Expand file tree Collapse file tree 2 files changed +117
-0
lines changed
Expand file tree Collapse file tree 2 files changed +117
-0
lines changed Original file line number Diff line number Diff line change 1+ # [ level 2] 소수 찾기 - 42839
2+
3+ [ 문제 링크] ( https://school.programmers.co.kr/learn/courses/30/lessons/42839 )
4+
5+ ### 성능 요약
6+
7+ 메모리: 95.3 MB, 시간: 22.82 ms
8+
9+ ### 구분
10+
11+ 코딩테스트 연습 > 완전탐색
12+
13+ ### 채점결과
14+
15+ 정확성: 100.0<br />합계: 100.0 / 100.0
16+
17+ ### 제출 일자
18+
19+ 2025년 08월 09일 15:07:01
20+
21+ ### 문제 설명
22+
23+ <p >한자리 숫자가 적힌 종이 조각이 흩어져있습니다. 흩어진 종이 조각을 붙여 소수를 몇 개 만들 수 있는지 알아내려 합니다.</p >
24+
25+ <p >각 종이 조각에 적힌 숫자가 적힌 문자열 numbers가 주어졌을 때, 종이 조각으로 만들 수 있는 소수가 몇 개인지 return 하도록 solution 함수를 완성해주세요.</p >
26+
27+ <h5 >제한사항</h5 >
28+
29+ <ul >
30+ <li >numbers는 길이 1 이상 7 이하인 문자열입니다.</li >
31+ <li >numbers는 0~9까지 숫자만으로 이루어져 있습니다.</li >
32+ <li >"013"은 0, 1, 3 숫자가 적힌 종이 조각이 흩어져있다는 의미입니다.</li >
33+ </ul >
34+
35+ <h5 >입출력 예</h5 >
36+ <table class =" table " >
37+ <thead><tr>
38+ <th >numbers</th >
39+ <th >return</th >
40+ </tr >
41+ </thead >
42+ <tbody><tr>
43+ <td >"17"</td >
44+ <td >3</td >
45+ </tr >
46+ <tr >
47+ <td >"011"</td >
48+ <td >2</td >
49+ </tr >
50+ </tbody >
51+ </table>
52+ <h5 >입출력 예 설명</h5 >
53+
54+ <p >예제 #1<br >
55+ [ 1, 7] 으로는 소수 [ 7, 17, 71] 를 만들 수 있습니다.</p >
56+
57+ <p >예제 #2<br >
58+ [ 0, 1, 1] 으로는 소수 [ 11, 101] 를 만들 수 있습니다.</p >
59+
60+ <ul >
61+ <li >11과 011은 같은 숫자로 취급합니다.</li >
62+ </ul >
63+
64+
65+ > 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Original file line number Diff line number Diff line change 1+ import java .util .*;
2+ class Solution {
3+ // 중복 방지
4+ Set <Integer > set = new HashSet <>();
5+ boolean [] visited ;
6+ public int solution (String numbers ) {
7+ char [] num = numbers .toCharArray ();
8+ visited = new boolean [num .length ];
9+
10+ for (int i =1 ; i <= num .length ; i ++){
11+ premutation (num ,"" ,i );
12+ }
13+
14+ int count = 0 ;
15+ for (int number : set ){
16+ if (isPrime (number )){
17+ count ++;
18+ }
19+ }
20+
21+ return count ;
22+ }
23+
24+ private boolean isPrime (int number ) {
25+ if (number <=1 ){
26+ return false ;
27+ }
28+ for (int i =2 ; i <= Math .sqrt (number ) ; i ++){
29+ if (number % i == 0 ){
30+ return false ;
31+ }
32+ }
33+ return true ;
34+ }
35+
36+ private void premutation (char [] num , String current , int maxLength ) {
37+ if (current .length () == maxLength ) {
38+ set .add (Integer .parseInt (current ));
39+ return ;
40+ }
41+
42+ for (int i = 0 ; i < num .length ; i ++){
43+ if (visited [i ] == false ){
44+ visited [i ] = true ;
45+ premutation (num ,current + num [i ],maxLength );
46+ visited [i ] = false ;
47+ }
48+ }
49+
50+
51+ }
52+ }
You can’t perform that action at this time.
0 commit comments