PAT甲级-质数、进制转换类型-1015 Reversible Primes解题思路

1015 Reversible Primes (20 分)

在这里插入图片描述

思路

经典进制转换 和 判断是否质数函数

代码

#include<bits/stdc++.h>
using namespace std;

bool isPrime(int n) //判断是否质数
{
    
    
    if(n<=1)return false;
    int sqr = (int)sqrt(n);
    for(int i = 2;i<=sqr;i++)
        if (n%i == 0)return false;
    return true;
}


int main(){
    
    
    int n,d;
    while(cin>>n)
    {
    
    
        int num[10000]={
    
    0};
        int len;
        if(n<0)break;
        cin>>d;
        if(!isPrime(n))cout<<"No"<<endl;
        else 
        {
    
    
            len = 0;
            do{
    
    
                num[len++] = n%d;
                n/=d;
            }while(n!=0);

            for(int i =0;i< len;i++)
                n = n*d + num[i];
            if(!isPrime(n))cout<<"No"<<endl;
            else cout<<"Yes"<<endl;

        }
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_43999137/article/details/114632976