[Programmers] Lv2. k์ง์์์ ์์ ๊ฐ์ ๊ตฌํ๊ธฐ | C++
2023. 6. 21. 15:52ใCoding Test/Programmers
๐๋ฌธ์ ๋ณด๋ฌ๊ฐ๊ธฐ
๐จ๐ปํ์ด ๊ณผ์
์์ ํ๋ณ ๋ถ๋ถ์์ int ์ค๋ฒํ๋ก์ฐ๊ฐ ๋ ์ ์์ผ๋ฏ๋ก, long long ์๋ฃํ์ผ๋ก ์์ฑํ์์ต๋๋ค. ์ ๋ถ๋ถ ๋๋ฌธ์ 20๋ถ์ ์๋ชจํ ๋ฏํด์...
1. n์ k์ง์๋ก ๋ฐ๊พธ์ด, ๋ฌธ์์ด๋ก ์ ์ฅํ๊ธฐ
2. k์ง์ ๋ฌธ์์ด์์ '0'์ ๊ธฐ์ค์ผ๋ก ๋ฌธ์์ด ๋ถ๋ฆฌํ๊ธฐ
3. ๊ฐ ๋ถ๋ฆฌ๋ ๋ฌธ์์ด๋ค์ ์์ ๊ฒ์ฌํ๊ธฐ
โ๏ธ์์ค ์ฝ๋ ๋ฐ ๊ฒฐ๊ณผ
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
using namespace std;
string GetKNumber(int n, int k) {
string number;
while (true) {
int quotient = n / k;
int remainder = n % k;
number.push_back(remainder + '0');
if (quotient == 0)
break;
n = quotient;
}
reverse(number.begin(), number.end());
return number;
}
bool IsPrimeNumber(long long n) {
if (n == 1)
return false;
for (long long i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
int solution(int n, int k) {
int answer = 0;
string kNum = GetKNumber(n, k);
stringstream tokenizer(kNum);
string num;
while (getline(tokenizer, num, '0')) {
if (!num.empty())
answer += IsPrimeNumber(stoll(num));
}
return answer;
}
728x90
๋ฐ์ํ
'Coding Test > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Programmers] Lv2. [3์ฐจ] n์ง์ ๊ฒ์ | C++ (0) | 2023.06.21 |
---|---|
[Programmers] Lv2. ๋ ๋งต๊ฒ | C++ (0) | 2023.06.21 |
[Programmers] Lv2. ํ๊ฒ ๋๋ฒ | C++ (0) | 2023.06.20 |
[Programmers] Lv2. ๋ด์ค ํด๋ฌ์คํฐ๋ง | C++ (0) | 2023.06.20 |
[Programmers] Lv2. ๊ธฐ๋ฅ๊ฐ๋ฐ | C++ (0) | 2023.06.16 |