zoukankan      html  css  js  c++  java
  • 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 (<) 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


    #include<iostream>
    #include<algorithm>
    #include<cstdio>
    #include<cstdlib>
    #include<cmath>
    #include<cstring>
    #include<string.h>
    using namespace std;
    const int maxn=110010;
    
    int isR(int n,int d){
        int m=0;
        int a[maxn];
        int i=0;
        while(n>0){
            a[i]=n%d;
            n/=d;
            i++;
        }
        for(int j=0;j<=i-1;j++){
            m=m*d+a[j];
        }
        return m;
    }
    
    bool isPrime(int n){
        if(n==0){
            return false;
        }
        if(n==1){    //1既不是素数也不是合数 
            return false;
        }
        for(int i=2;i<=sqrt(n);i++){    //注意:i<=sqrt(n) 否则4判错 
            if(n%i==0){
                return false;
            }
        }
        return true;
    }
    
    int main(){
        int n,d;
        int a,b;
        while(scanf("%d",&n)){
            a=0;
            b=0;
            if(n<0){
                return 0;
            }
            scanf("%d",&d);
            if(isPrime(n)){
                a=1;
            }
            n=isR(n,d);
            if(isPrime(n)){
                b=1;
            }
            if(a==1&&b==1){
                printf("Yes
    ");
            }else{
                printf("No
    ");
            }
        }
        return 0;
    } 
  • 相关阅读:
    近期C#小问题总结
    Arcgis由栅格数据提取等值线
    Arcgis由离散点制作核密度图
    用Arcgis为离散点区域生成格网(渔网)
    Arcengine合并面要素
    对离散点进行抽稀
    对离散点进行区域分割
    关于异常来自 HRESULT:0x80040351
    分布式缓存---Memcached 入门
    Mongodb安装 for windows7 64位
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14279576.html
Copyright © 2011-2022 走看看