• 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 }
  • 相关阅读:
    linux定时器的使用
    6410 spi总线
    6410 spi 设备驱动
    qtcreator 安装后不能显示菜单
    基于Tiny 6410的内核移植 (NAND FLASH 、UBIFS篇)
    有关ajax跨域问题
    sqlmap的安装
    C# 连接数据库的配置方法
    初识Markdown
    ASP.NET 使用Ueditor富文本编辑器
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11192122.html
Copyright © 2020-2023  润新知