• PAT甲级——A1015 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 (<) and D (1), 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

    其实就是将输入的数字转化为该进制的反序列,然后再转化为10进制
    然后判断是否是素数即可

     1 #include <iostream>
     2 #include <math.h>
     3 #include <string>
     4 
     5 using namespace std;
     6 
     7 bool isPrim(int a)
     8 {
     9     if (a < 2)
    10         return false;
    11     int b = (int)sqrt(1.0 * a);
    12     for (int i = 2; i <= b; ++i)
    13     {
    14         if (a%i == 0)
    15             return false;
    16     }
    17     return true;
    18 }
    19 
    20 int getReverNum(int a, int b)
    21 {
    22     string str = "";//得到反转的序列
    23     while (a > 0)
    24     {
    25         str += a % b + '0';
    26         a /= b;
    27     }
    28     int n = 0;
    29     for (int i = str.length() - 1, j = 0; i >= 0; --i, ++j)
    30         n += (str[i] - '0')*pow(b, j);
    31     return n;
    32 }
    33 
    34 int main()
    35 {
    36     int a, b;
    37     while (cin >> a)
    38     {
    39         if (a < 0)break;
    40         cin >> b;
    41         if (isPrim(a) && isPrim(getReverNum(a,b)))
    42             cout << "Yes" << endl;
    43         else
    44             cout << "No" << endl;
    45     }
    46     return 0;
    47 }
  • 相关阅读:
    P1182 数列分段`Section II`
    算法整理:Floyd_多源最短路
    【FBI WARNING】递归(高级数据结构的基础)
    【FBI WARNING】DP 从看透到看开
    两个例题
    结构体
    环状序列(Circular Sequence, ACM/ICPC Seoul 2004, UVa1584)
    生成元(Digit Generator, ACM/ICPC Seoul 2005, UVa1583)
    猜数字游戏的提示(Master-Mind Hints, UVa 340)
    回文词(Palindromes, UVa401)
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11192122.html
Copyright © 2020-2023  润新知