π§ μ½λ©ν
μ€νΈ
[μΈνλ° C++] 14. λ€μ§μ μμ
peewoong
2024. 3. 21. 19:00
Nκ°μ μμ°μκ° μ λ ₯λλ©΄ κ° μμ°μλ₯Ό λ€μ§μ ν κ·Έ λ€μ§μ μκ° μμμ΄λ©΄ κ·Έ μλ₯ΌμΆλ ₯νλ νλ‘κ·Έλ¨μ μμ±νμΈμ. μλ₯Ό λ€μ΄ 32λ₯Ό λ€μ§μΌλ©΄ 23μ΄κ³ , 23μ μμμ΄λ€. κ·Έλ¬λ©΄ 23μ μΆλ ₯νλ€. λ¨ 910μ λ€μ§μΌλ©΄, 19λ‘ μ«μνν΄μΌνλ€. 첫 μ리λΆν°μ μ°μλ 0μ 무μνλ€. λ€μ§μ ν¨μμΈ int reverse(int x)μ μμμΈμ§ νμΈνλ ν¨μ bool isPrime(int x)λ₯Ό λ°λμ μμ±νλ€.
μ λ ₯
5
32 55 62 3700 250
μΆλ ₯
23 73
#include <iostream>
using namespace std;
int reverse(int x){
int res, tmp;
while(x>0){
tmp = x % 10;
res = res * 10 + tmp;
x = x / 10;
}
return res;
}
bool isPrime(int x){
bool isPrime = true;
if(x==1) return false;
for(int i = 2; i < x; i++){
if(x%i == 0){
isPrime = false;
}
}
return isPrime;
}
int main(void){
int n, num, temp;
cin >> n;
for(int i = 0; i < n; ++i){
cin >> num;
temp = reverse(num);
if(isPrime(temp)){
cout << temp;
}
}
return 0;
}