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;
    } 
  • 相关阅读:
    架构中的技术性解决难题
    设计一个js的架构第二篇
    DOCTYPE 严格模式与混杂模式
    架构中的技术性解决难题之解决篇
    css常用页面布局
    记录一个css的综合运用
    写在立春
    Win7重装后,如何删除cygwin目录?
    重读《The C Programming Language》
    [分享]多个选项卡切换效果
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14279576.html
Copyright © 2011-2022 走看看