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;
    } 
  • 相关阅读:
    财务自由之路--笔记
    Spring Cloud微服务笔记(一)微服务与云概念
    Java8新特性----Stream
    Docker操作笔记(四)使用网络
    Docker操作笔记(三)数据管理
    Docker操作笔记(二)容器
    Docker操作笔记(一)使用镜像
    信息化领域热词分类分析及解释
    假期第二周总结
    假期第一周总结
  • 原文地址:https://www.cnblogs.com/dreamzj/p/14279576.html
Copyright © 2011-2022 走看看