PTA(Advanced Level)1015.Reversible Primes

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
思路

题目的大意是判断一个数本身是否为素数,以及它在进制\(D\)下是否为素数

  • 检查素数的方法和根据进制反转没有什么好讲的,挺简单的
  • 值得一提的是本身不是素数就没必要再进行反转了
代码
#include<bits/stdc++.h>
using namespace std;
bool is_prime(int x)
{
    if(x <= 1)  return false;
    int up = (int)sqrt(x * 1.0);
    for(int i=2;i<=up;i++)
        if(x % i == 0)
            return false;
    return true;
}   //判断素数的方法

int getR(int n, int d)
{
    vector<int> v;
    int t = 0;
    while(n)
    {
        t = n % d;
        v.push_back(t);
        n /= d;
    }
    int r_value = 0;
    for(int i=0;i<v.size();i++)
    {
        r_value += v[i] * pow(d, v.size() - i - 1);
    }
    return r_value;
}   //根据进制得到反转之后的值

int main()
{
    int n, d;
    while(cin >> n)
    {
        if(n < 0)   break;
        else    cin >> d;
        if(!is_prime(n))    //如果本身不是素数,没有进一步检查的必要
        {
            cout << "No" << endl;
            continue;
        }
        else
        {
            int reverse_value = getR(n,d);
            if(is_prime(reverse_value))
                cout << "Yes" << endl;
            else cout << "No" << endl;
        }
    }
    return 0;
}
引用

https://pintia.cn/problem-sets/994805342720868352/problems/994805495863296000

原文地址:https://www.cnblogs.com/MartinLwx/p/12527248.html

时间: 2024-10-06 08:25:22

PTA(Advanced Level)1015.Reversible Primes的相关文章

1015. Reversible Primes (20) ——PAT (Advanced Level) Practise

题目信息: 1015. Reversible Primes (20) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue 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

PAT 1015 Reversible Primes[求d进制下的逆][简单]

1015 Reversible Primes (20)(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

pat 1015 Reversible Primes(20 分)

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 a

PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))

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

PTA(Advanced Level)1036.Boys vs Girls

This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students. Input Specification: Each input file contains one test case. Each case contains a positive integer N, f

1015. Reversible Primes

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 (&

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 (&

PAT 1015. Reversible Primes

#include <cstdio> #include <cstdlib> #include <cstring> #include <vector> using namespace std; void init_primes(vector<char>& primes) { int len = primes.size(); for (int i=0; i<len && i<2; i++) primes[i] = f

1015 Reversible Primes (20)(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 (&