• 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 (<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,2,3和负数的情况

    #include<bits/stdc++.h>
    #define de(x) cout<<#x<<" "<<(x)<<endl
    #define each(a,b,c) for(int a=b;a<=c;a++)
    using namespace std;
    const int maxn=50+5;
    int buf[maxn];
    bool isprime(int x)
    {
        if(x<=1)return false;
        bool flag=true;
        if(x==2)return true;
        if(x==3)return true;
        for(int i=2;i*i<=x;i++)
        {
            if(x%i==0)
            {
                flag=false;
                break;
            }
        }
        return flag;
    }
    int main()
    {
        //de(isprime(73));
        int n,r;
        while(true)
        {
            cin>>n;
            if(n<0)break;
            cin>>r;
            if(n==1||n==0)
            {
                puts("No");
                continue;
            }
            if(!isprime(n))///读题的问题,首先得判断他是不是一个质数才行,
            {
                puts("No");
                continue;
            }
            int len=0;
            while(n>0)
            {
                buf[len++]=n%r;
                n/=r;
            }
            //de(len);
            int sum=0;
            len--;
            for(int i=0;i<=len;i++)
            {
                sum+=buf[i]*pow(r,len-i);
            }
            //de(sum);
            if(isprime(sum))puts("Yes");
            else puts("No");
        }
    }
    
    
  • 相关阅读:
    PAT (Advanced Level) 1086. Tree Traversals Again (25)
    PAT (Advanced Level) 1085. Perfect Sequence (25)
    PAT (Advanced Level) 1084. Broken Keyboard (20)
    PAT (Advanced Level) 1083. List Grades (25)
    PAT (Advanced Level) 1082. Read Number in Chinese (25)
    HDU 4513 吉哥系列故事――完美队形II
    POJ Oulipo KMP 模板题
    POJ 3376 Finding Palindromes
    扩展KMP
    HDU 2289 Cup
  • 原文地址:https://www.cnblogs.com/Tony100K/p/11758029.html
Copyright © 2020-2023  润新知