PAT 1015 Reversible Primes(进制转换+素数判断)

PAT 1015

1015 Reversible Primes (20 分)

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 (<10​5​​) and D (1

Input Specification:

The input file consists of several test cases. Each case occupies a line which contains two integers N and D. The input is finished by a negative N.

Output Specification:

For each test case, print in one line Yes if N is a reversible prime with radix D, or No if not.

Sample Input:

73 10
23 2
23 10
-2

Sample Output:

Yes
Yes
No

 

题意有点不好理解啊,一开始读错题了。

题意:判断一个数是否为素数且将它转化为d进制后,将d进制倒过来重新组合为十进制形成一个新的数x,再判断x是否为素数,

如果同时满足则输出yes否则no

 

用一个数组记录出转化为d进制的数即可。

#include
#include
#include
#include
#include
#include
using namespace std;
const int MAXN(1000);
typedef long long int LL;
int a[20];
LL change(int n,int d) {
    int len=0;
    while(n) {
        a[++len]=n%d;
        n/=d;
    }
    LL res=0;
    int index=0;
    for(int i=len;i>=1;i--) {
        res+=(pow(d,index++)*a[i]);
    }
    return res;
}
bool judge(LL x) {
    if(x==1) return false;
    for(int i=2;i*i<=x;i++) {
        if(x%i==0) {
            return false;
        }
    }
    return true;
}
int main() {
    int n,d;
    while(~scanf("%d",&n)&&n>=0) {
        scanf("%d",&d);
        LL temp=change(n,d);
        if(judge(n)==true&&judge(temp)==true)
            cout<<"Yes"<

 

你可能感兴趣的:(PAT)