본문 바로가기
Coding Test/Programmers

[Programmers] (2022 KAKAO BLIND RECRUITMENT) Lv 2. k진수에서 소수 개수 구하기

by song.ift 2023. 6. 26.

https://school.programmers.co.kr/learn/courses/30/lessons/92335

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

#include <string>
#include <vector>
#include <cmath>

using namespace std;

bool IsPrime(long param)
{
    for (int i = 2; i <= sqrt(param); ++i)
        if (!(param % i)) return false;
    return (param > 1);
}

int solution(int n, int k) {
    int answer = 0;

    string str = "";
    while (n)
    {
        int temp = n % k;
        if (temp) str = to_string(temp) + str;
        else if (!str.empty()) answer += IsPrime(stol(str)) ? 1 : 0, str.clear();
        
        n /= k;
    }

    if (!str.empty()) answer += IsPrime(stol(str)) ? 1 : 0; // 마지막까지 남은 str
    return answer;
}

GitHub : https://github.com/developeSHG/Algorithm-Baekjoon_Programmers/blob/68587d69805d0f2ccef2d01930512d23c40e19fe/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4/lv2/92335.%E2%80%85k%EC%A7%84%EC%88%98%EC%97%90%EC%84%9C%E2%80%85%EC%86%8C%EC%88%98%E2%80%85%EA%B0%9C%EC%88%98%E2%80%85%EA%B5%AC%ED%95%98%EA%B8%B0/k%EC%A7%84%EC%88%98%EC%97%90%EC%84%9C%E2%80%85%EC%86%8C%EC%88%98%E2%80%85%EA%B0%9C%EC%88%98%E2%80%85%EA%B5%AC%ED%95%98%EA%B8%B0.cpp

 

GitHub - developeSHG/Algorithm-Baekjoon_Programmers: 백준 and 프로그래머스 소스코드

백준 and 프로그래머스 소스코드. Contribute to developeSHG/Algorithm-Baekjoon_Programmers development by creating an account on GitHub.

github.com

 

댓글