• 1015. Reversible Primes (20)


    题目连接:https://www.patest.cn/contests/pat-a-practise/1015

    题目如下:

    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
    
    这道题要得揣摩出输入的N是十进制的……
    大致有两种方法,一种是将输入的数N不断除以基数,并将每次得到的余数存入数组中,然后计算得出反转后的数,判断即可;
    还有一种是推到出迭代公式,这个公式我实在是自己推不出来,希望哪位网友能帮忙回答下这个迭代公式是怎么来的……
    代码一(数组):
     1 #include<stdio.h>
     2 #include<math.h>
     3 #define MAXN 100005
     4 
     5 int rev[MAXN];
     6 int IsPrime(int num)
     7 {
     8     if (num<2)return 0;
     9     else if (num==2 ||num==3)return 1;
    10     if (num%2==0)return 0;
    11     int i;
    12     for (i=3;i<=sqrt(num);i=i+2)
    13     {
    14         if (num%i==0) return  0;
    15     }
    16     return 1;
    17 }
    18 
    19 int main()
    20 {
    21     int num,d,i=0,ori,result=0;
    22     scanf("%d",&num);
    23     while (num>=0)
    24     {
    25         ori=num;
    26         scanf("%d",&d);
    27         int radix=1;
    28         do
    29         {
    30             rev[i++]=num%d;
    31             num/=d;
    32         }while (num);
    33         for (--i;i>=0;i--)
    34         {
    35             result+=radix*rev[i];
    36             radix*=d;
    37         }
    38         if (IsPrime(ori) && IsPrime(result))printf("Yes
    ");
    39         else printf("No
    ");
    40         result=0;
    41         i=0;
    42         scanf("%d",&num);
    43     }
    44 }
    View Code

    代码二(迭代公式):

     1 #include<stdio.h>
     2 #include<math.h>
     3 
     4 int IsPrime(int num)
     5 {
     6     if (num<2)return 0;
     7     if (num==2 || num==3)return 1;
     8     if (num%2==0)return 0;
     9     int i;
    10     for (i=3;i<=sqrt(num);i+=2)
    11     {
    12         if (num%i==0)return 0;
    13     }
    14     return 1;
    15 }
    16 
    17 int main()
    18 {
    19     int num,d,ori,result=0;
    20     scanf("%d",&num);
    21     while (num>=0)
    22     {
    23         scanf("%d",&d);
    24         ori=num;
    25         if (IsPrime(ori))
    26         {
    27             do
    28             {
    29                 result=result*d+num%d;
    30                 num/=d;
    31             }while(num);
    32             if (IsPrime(result))printf("Yes
    ");
    33             else printf("No
    ");
    34         }
    35         else printf("No
    ");
    36         scanf("%d",&num);
    37         result=0;
    38     }
    39     return 0;
    40 }
    View Code
    
    
  • 相关阅读:
    OA系统配置文件
    OA系统配置文件
    OA系统配置文件
    OA系统配置文件
    如何在github上递交高质量的pull request
    如何在github上递交高质量的pull request
    如何在github上递交高质量的pull request
    如何在github上递交高质量的pull request
    Python 变量作用域
    python match search 和不匹配
  • 原文地址:https://www.cnblogs.com/wuxiaotianC/p/6351566.html
Copyright © 2020-2023  润新知