1015 Reversible Primes (20 分)(素数)

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.

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

生词

英文 解释
reversible 可逆的
radix 进制

题目大意:

如果一个数本身是素数,而且在d进制下反转后的数在十进制下也是素数,就输出Yes,否则就输出No

分析:

判断输入是否为负数,判断n是否为素数,把n转换为d进制再反过来转换为10进制,判断是否为素数

原文链接:https://blog.csdn.net/liuchuo/article/details/52155670

题解

这题重点要理解题意:
输一个N,一个进制Radix,首先判断N是不是素数,然后先转换成Radix进制,然后倒叙,然后转换成10进制判断是不是素数。
因为没get题意,鼓捣了半天的easy题,,,

#include <bits/stdc++.h>

using namespace std;
bool prime(int a){
    if(a<=1) return false;
    for(int i=2;i*i<=a;i++){
        if(a%i==0) return false;
    }
    return true;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("1.txt", "r", stdin);
#endif
    int N,D;
    while(true){
        cin>>N;
        vector<int> v;
        if(N<0) break;
        cin>>D;
        if(!prime(N)){
            cout<<"No"<<endl;
            continue;
        }
        while(N){
            v.push_back(N%D);
            N/=D;
        }
        for(int i=0;i<v.size();i++){
            N=N*D+v[i];
        }
        if(prime(N)) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}

本文来自博客园,作者:勇往直前的力量,转载请注明原文链接:https://www.cnblogs.com/moonlight1999/p/15518771.html

原文地址:https://www.cnblogs.com/moonlight1999/p/15518771.html