Skip to content

Commit 05e87ab

Browse files
committed
[level 2] Title: k진수에서 소수 개수 구하기, Time: 8.91 ms, Memory: 3.63 MB -BaekjoonHub
1 parent eb662a4 commit 05e87ab

2 files changed

Lines changed: 147 additions & 0 deletions

File tree

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# [level 2] k진수에서 소수 개수 구하기 - 92335
2+
3+
[문제 링크](https://school.programmers.co.kr/learn/courses/30/lessons/92335)
4+
5+
### 성능 요약
6+
7+
메모리: 3.63 MB, 시간: 8.91 ms
8+
9+
### 구분
10+
11+
코딩테스트 연습 > 2022 KAKAO BLIND RECRUITMENT
12+
13+
### 채점결과
14+
15+
정확성: 100.0<br/>합계: 100.0 / 100.0
16+
17+
### 제출 일자
18+
19+
2026년 05월 02일 12:15:10
20+
21+
### 문제 설명
22+
23+
<h5>문제 설명</h5>
24+
25+
<p>양의 정수 <code>n</code>이 주어집니다. 이 숫자를 <code>k</code>진수로 바꿨을 때, 변환된 수 안에 아래 조건에 맞는 소수(Prime number)가 몇 개인지 알아보려 합니다.</p>
26+
27+
<ul>
28+
<li><code>0P0</code>처럼 소수 양쪽에 0이 있는 경우</li>
29+
<li><code>P0</code>처럼 소수 오른쪽에만 0이 있고 왼쪽에는 아무것도 없는 경우</li>
30+
<li><code>0P</code>처럼 소수 왼쪽에만 0이 있고 오른쪽에는 아무것도 없는 경우</li>
31+
<li><code>P</code>처럼 소수 양쪽에 아무것도 없는 경우</li>
32+
<li>단, <code>P</code>는 각 자릿수에 0을 포함하지 않는 소수입니다.
33+
34+
<ul>
35+
<li>예를 들어, 101은 <code>P</code>가 될 수 없습니다.</li>
36+
</ul></li>
37+
</ul>
38+
39+
<p>예를 들어, 437674을 3진수로 바꾸면 <code>211</code>0<code>2</code>01010<code>11</code>입니다. 여기서 찾을 수 있는 조건에 맞는 소수는 왼쪽부터 순서대로 211, 2, 11이 있으며, 총 3개입니다. (211, 2, 11을 <code>k</code>진법으로 보았을 때가 아닌, 10진법으로 보았을 때 소수여야 한다는 점에 주의합니다.) 211은 <code>P0</code> 형태에서 찾을 수 있으며, 2는 <code>0P0</code>에서, 11은 <code>0P</code>에서 찾을 수 있습니다.</p>
40+
41+
<p>정수 <code>n</code>과 <code>k</code>가 매개변수로 주어집니다. <code>n</code>을 <code>k</code>진수로 바꿨을 때, 변환된 수 안에서 찾을 수 있는 <strong>위 조건에 맞는 소수</strong>의 개수를 return 하도록 solution 함수를 완성해 주세요.</p>
42+
43+
<hr>
44+
45+
<h5>제한사항</h5>
46+
47+
<ul>
48+
<li>1 ≤ <code>n</code> ≤ 1,000,000</li>
49+
<li>3 ≤ <code>k</code> ≤ 10</li>
50+
</ul>
51+
52+
<hr>
53+
54+
<h5>입출력 예</h5>
55+
<table class="table">
56+
<thead><tr>
57+
<th>n</th>
58+
<th>k</th>
59+
<th>result</th>
60+
</tr>
61+
</thead>
62+
<tbody><tr>
63+
<td>437674</td>
64+
<td>3</td>
65+
<td>3</td>
66+
</tr>
67+
<tr>
68+
<td>110011</td>
69+
<td>10</td>
70+
<td>2</td>
71+
</tr>
72+
</tbody>
73+
</table>
74+
<hr>
75+
76+
<h5>입출력 예 설명</h5>
77+
78+
<p><strong>입출력 예 #1</strong></p>
79+
80+
<p>문제 예시와 같습니다. </p>
81+
82+
<p><strong>입출력 예 #2</strong></p>
83+
84+
<p>110011을 10진수로 바꾸면 110011입니다. 여기서 찾을 수 있는 조건에 맞는 소수는 11, 11 2개입니다. 이와 같이, 중복되는 소수를 발견하더라도 모두 따로 세어야 합니다.</p>
85+
86+
<h5>문제가 잘 안풀린다면😢</h5>
87+
88+
<p>힌트가 필요한가요? [코딩테스트 연습 힌트 모음집]으로 오세요! → <a href="https://school.programmers.co.kr/learn/courses/14743?itm_content=lesson92335" target="_blank" rel="noopener">클릭</a></p>
89+
90+
91+
> 출처: 프로그래머스 코딩 테스트 연습, https://school.programmers.co.kr/learn/challenges
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include <string>
2+
#include <vector>
3+
#include <algorithm>
4+
#include <math.h>
5+
#include <iostream>
6+
#include <set>
7+
8+
using namespace std;
9+
10+
11+
string toJinsu(int n, int k){
12+
string tmp = "";
13+
14+
while(n>0){
15+
tmp += to_string(n%k);
16+
n /= k;
17+
}
18+
reverse(tmp.begin(), tmp.end());
19+
return tmp;
20+
}
21+
22+
bool isPrime(long long num){
23+
if(num < 2) return false;
24+
25+
int cnt = 0;
26+
for(long long i=2 ; i<=sqrt(num); i++){
27+
if(num % i == 0) return false;
28+
}
29+
return true;
30+
}
31+
32+
33+
int solution(int n, int k) {
34+
int answer = 0;
35+
36+
string jinsu = toJinsu(n, k);
37+
string tmp = "";
38+
39+
for(char c : jinsu){
40+
if(c == '0'){
41+
if( !tmp.empty()){
42+
if(isPrime(stoll(tmp))) answer++;
43+
tmp = "";
44+
}
45+
}
46+
else{
47+
tmp += c;
48+
}
49+
}
50+
51+
if(!tmp.empty()){
52+
if(isPrime(stoll(tmp))) answer++;
53+
}
54+
55+
return answer;
56+
}

0 commit comments

Comments
 (0)