• 1015. Reversible Primes


    1015. Reversible Primes (20)

    时间限制
    400 ms
    内存限制
    32000 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    CHEN, Yue

    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
    
      1 #include <iostream>
    2 #include <fstream>
    3 #include <vector>
    4 #include <string>
    5 #include <algorithm>
    6 #include <map>
    7 #include <stack>
    8 #include <cmath>
    9 #include <queue>
    10 #include <set>
    11
    12
    13 using namespace std;
    14
    15
    16
    17 bool isPrime( int x )
    18 {
    19 if( x < 2 )
    20 {
    21 return false;
    22 }
    23 else if( x == 2 || x == 3 )
    24 {
    25 return true;
    26 }
    27 else if( x%2 == 0 )
    28 {
    29 return false;
    30 }
    31 else
    32 {
    33
    34 for( int i = 2 ; i * i <= x ; ++i )
    35 {
    36 if( x % i == 0 )
    37 {
    38 return false;
    39 }
    40 }
    41
    42 return true;
    43 }
    44 }
    45
    46
    47
    48 int main()
    49 {
    50
    51
    52
    53 int N;
    54 int D;
    55
    56 while( cin >> N )
    57 {
    58 if( N < 0 )
    59 {
    60 break;
    61 }
    62 else
    63 {
    64 cin >> D;
    65
    66 if( isPrime(N) )
    67 {
    68
    69 int n = N;
    70
    71 queue<int> queue;
    72
    73
    74 while( n != 0 )
    75 {
    76 queue.push(n%D);
    77 n = n / D;
    78 }
    79
    80 int reverse = 0;
    81
    82 while(!queue.empty())
    83 {
    84 reverse*= D;
    85 reverse += queue.front();
    86 queue.pop();
    87 }
    88
    89 if( isPrime( reverse ) )
    90 {
    91 cout << "Yes" << endl;
    92 }
    93 else
    94 {
    95 cout << "No" << endl;
    96 }
    97
    98 }
    99 else
    100 {
    101 cout << "No" << endl;
    102 }
    103
    104
    105
    106 }
    107 }
    108
    109
    110
    111
    112 return 0;
    113 }


  • 相关阅读:
    Python 列表元素排重uniq
    Python正则表达式汇总
    Python 正则表达式:只要整数和小数
    c++写入txt文件
    OpenMP求完数
    Python分割list
    用ConfigParser模块读写配置文件——Python
    Python 正则表达式
    教程和工具--用wxPython编写GUI程序的
    matlab 之字体调整
  • 原文地址:https://www.cnblogs.com/kking/p/2331812.html
Copyright © 2020-2023  润新知