PAT A1015 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 (<10?5??) 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

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <algorithm>
#include <iostream>
#include <string.h>
#include <queue>
#include <string>
using namespace std;
const int maxn = 1010;
int n, d;
int to_10(int i,int radix) {
    int res = 0,exp=0;
    while (i != 0) {
        res += i%10*pow(radix, exp);
        exp++;
        i /= 10;
    }
    return res;
}
bool is_prime(int i) {
    if (i == 0 || i == 1)return false;
    if (i == 2 || i == 3)return true;
    for (int j = 2; j*j <= i; j++) {
        if (i%j == 0)return false;
    }
    return true;
}
string to_s(int i,int radix) {
    string s = "";
    do {
        s += ‘0‘ + i % radix;
        i /= radix;
    } while (i != 0);
    reverse(s.begin(), s.end());
    return s;
}
int to_num(string s) {
    int res = 0;
    for (int i = s.length()-1; i >=0; i--) {
        res = res * 10 + s[i] - ‘0‘;
    }
    return res;
}
int main() {
    while (true) {
        scanf("%d", &n);
        if (n < 0)break;
        else {
            scanf("%d", &d);
            if (is_prime(n) && is_prime(to_10(to_num(to_s(n,d)), d))) printf("Yes\n");
            else printf("No\n");
        }
    }
    system("pause");
    return 0;
}

注意点:题目没看懂,导致结果一直错。题目的意思是一个10进制数如果他本身是素数,然后转换到给定进制下并将其反转,再转化到10进制,还是素数的话即为 Yes。

原文地址:https://www.cnblogs.com/tccbj/p/10385391.html

时间: 2024-10-03 02:32:46

PAT A1015 Reversible Primes (20 分)的相关文章

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

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

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

pat1015. Reversible Primes (20)

1015. Reversible Primes (20) 时间限制 400 ms 内存限制 65536 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 is 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 a

pat 1035 Password(20 分)

1035 Password(20 分) To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase), or 0 (zero) fro

pat 1077 Kuchiguse(20 分) (字典树)

1077 Kuchiguse(20 分) The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker's personality. Such a preference is called "Kuchiguse" and is often

[PTA] PAT(A) 1008 Elevator (20 分)

目录 Problem Description Input Output Sample Sample Input Sample Output Solution Analysis Code Problem portal: 1008 Elevator (20 分) Description  The highest building in our city has only one elevator. A request list is made up with $N$ positive numbers