-
[프로그래머스] Level2 - k진수에서 소수 구하기코딩 테스트 2022. 10. 6. 12:50
1. 문제 설명
Link : https://school.programmers.co.kr/learn/courses/30/lessons/92335
2. 나의 풀이
#include <string> #include <vector> #include <sstream> #include <stack> using namespace std; string ToNotation(int number, int notation) { stack<int> stk; while(number >= 1) { stk.push(number % notation); number = number / notation; } string ret = ""; while (!stk.empty()) { ret += to_string(stk.top()); stk.pop(); } return ret; } bool IsPrime(long long num) { if (num == 0 || num == 1) return false; for (long i = 2; i * i <= num; ++i) { if (num % i == 0) return false; } return true; } int solution(int n, int k) { string num = ToNotation(n, k); stringstream ss(num); string buf; int answer = 0; while(getline(ss, buf, '0')) { if (buf.empty()) continue; if (IsPrime(stoll(buf))) ++answer; } return answer; }
진법 변환을 하고 나면 자리수가 매우 커질 수 있으므로 long long을 사용해야 런타임 오류를 피할 수 있다.
코딩 테스트 답안은 GitHub에도 업로드합니다.
GitHub Link : https://github.com/KimNamWook316/CodingTestSolved
'코딩 테스트' 카테고리의 다른 글
[프로그래머스] Level2 - 주차 요금 계산 (2) 2022.10.05 [프로그래머스] Level2 - 두 큐 합 같게 만들기 (1) 2022.09.29 [프로그래머스] Level1 - 신고 결과 받기 (0) 2022.09.27 [프로그래머스] Level1 - 완주하지 못한 선수 (0) 2022.09.20 [프로그래머스] Level1 - 다트 게임 (0) 2022.09.15