https://school.programmers.co.kr/learn/courses/30/lessons/92335
프로그래머스
코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.
programmers.co.kr
Lv. 2
2022 KAKAO BLIND RECRUITMENT
주석으로 설명을 대체합니다.
#include <string>
#include <vector>
#include <cmath>
using namespace std;
bool isPrime(long long num) {
if (num < 2)
return false;
for (int i = 2; i <= sqrt(num); i++) {
if (num % i == 0)
return false;
}
return true;
}
int solution(int n, int k) {
int answer = 0;
// 10진수를 k진수로 변환
string str = "";
while (n > 0) {
str = to_string(n % k) + str;
n /= k;
}
// 소수인지 판단
string tmp = "";
for (char c : str) {
if (c == '0' && !tmp.empty()) {
if (isPrime(stoll(tmp)))
answer++;
tmp.clear();
} else
tmp += c;
}
// 마지막 남은 tmp 검사
if (!tmp.empty() && isPrime(stoll(tmp)))
answer++;
return answer;
}'Algorithm' 카테고리의 다른 글
| [프로그래머스/C++] 의상 (0) | 2024.10.31 |
|---|---|
| [프로그래머스/C++] 더 맵게 (0) | 2024.03.15 |
| [프로그래머스/C++] [3차] n진수 게임 (0) | 2024.03.11 |
| [프로그래머스/C++] 전화번호 목록 (0) | 2024.03.02 |
| [프로그래머스/C++] [1차] 뉴스 클러스터링 (0) | 2024.03.01 |