一.十进制向N进制转换
设进制数为X,X while 除以N,直至X为0
二.N进制向十进制转换
从第零位依次乘N的位数次方即可
三.数的翻转
我们一般借助的是队列,先进先出,首位先进,首位先出进行处理
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
int n,radix;
bool isPrime(int x)
{
if(x == 1 || x == 0) return false;
if(x == 2) return true;
if(x % 2 == 0) return false;
for(int i = 3; i <= x/2; i += 2)
{
if(x % i == 0) return false;
}
return true;
}
queue<int> q;
int main()
{
int temp;
while(scanf("%d",&n) && n >= 0)
{
scanf("%d",&radix);
if(!isPrime(n)) printf("No\n");
else
{
temp = 0;
while(!q.empty()) q.pop();
while(n)
{
q.push(n%radix);
n /= radix;
}
while(!q.empty())
{
temp = q.front() + temp * radix;
q.pop();
}
if(isPrime(temp)) printf("Yes\n");
else printf("No\n");
}
}
return 0;
}