[ 문제 ]
[ 접근방법 ]
소수 및 팰린드롬을 판별하는 함수를 따로 작성한다.
[ 소스코드 ]
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int MAX_N = 1003001;
int n;
bool isPrime[MAX_N + 1];
void initPrime()
{
fill(isPrime, isPrime + MAX_N + 1, 1);
isPrime[0] = isPrime[1] = 0;
for (int i = 2; i * i <= MAX_N; i++)
{
if (isPrime[i])
{
for (long long j = i * i; j <= MAX_N; j += i)
{
isPrime[j] = 0;
}
}
}
}
bool isPalindrome(int x)
{
string str = to_string(x);
string streverse = str;
reverse(streverse.begin(), streverse.end());
return str == streverse;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
initPrime();
for (int i = n;; i++)
{
if (isPrime[i] == 1 && isPalindrome(i))
{
cout << i;
break;
}
}
return 0;
}
'PS' 카테고리의 다른 글
[ 백준 / C++ ] 4375 : 1 (0) | 2024.08.06 |
---|---|
[ 백준 / C++ ] 7662 : 이중 우선순위 큐 (0) | 2024.08.05 |
[ 백준 / C++ ] 2503 : 숫자 야구 (0) | 2024.08.01 |
[ 백준 / C++ ] 1652 : 누울 자리를 찾아라 (0) | 2024.07.31 |
[ 백준 / C++ ] 10868 : 최솟값 (0) | 2024.07.29 |