1015 Reversible Primes (20 point(s))
A reversible prime in any number system is a prime whose "reverse" in that number system is also a prime. For example in the decimal system 73 is a reversible prime because its reverse 37 is also a prime.
Now given any two positive integers N (<105) and D (1<D≤10), you are supposed to tell if N is a reversible prime with radix D.
Example:
#include<iostream>
#include<queue>
using namespace std;
bool isPrime(int num)
{
if(num <= 1) return false;
for(int i = 2; i*i <= num; i++)
if(num % i == 0) return false;
return true;
}
int main()
{
while(1) {
int N, K;
cin >> N;
if(N < 0) break;
cin >> K;
if(isPrime(N)) {
queue<int> digits;
while(N) {
digits.push(N % K);
N /= K;
}
while(!digits.empty()) {
N *= K;
N += digits.front();
digits.pop();
}
if(isPrime(N)) {
cout << "Yes\n";
continue;
}
}
cout << "No\n";
}
}
思路:
利用队列,将数字翻转,再判断是否素数。