zoukankan      html  css  js  c++  java
  • 1015. Reversible Primes (20)

    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

    题目大意:判断原数字是否是素数,倒序后是否是素数,如果都是素数输出Yse

                     这里的是指原数字与它在数制下的倒序,如23 2,是10111(2)的倒叙11101的真实数字。

     1 #include<stdio.h>
     2 #include<math.h>
     3 #include<stdlib.h>
     4 int in[6];
     5 int main()
     6 {
     7     int n,d,m,temp;
     8     int i,j;
     9     int sum,flag;
    10     while( scanf("%d",&n)!=EOF)
    11     {
    12         if( n<0 ) break;
    13         scanf("%d",&d);
    14         i=0;  //分解整数的下标
    15         sum=0;  //转换后的值
    16         flag = 0;  //标记是否能转换
    17         temp=n;   //保存n的值
    18         
    19         while( n ) //分解位数
    20         {
    21             in[i++]=n%d;
    22             n /= d;
    23         }
    24         for( j=0; j<i ; j++) //求转换的值
    25             sum = sum*d+ in[j];
    26         if( isPrime(sum) && isPrime(temp)) //如果转换前和转换后都是素数
    27             flag =1;
    28         if( !flag) printf("No
    ");
    29         else printf("Yes
    ");
    30     }
    31     return 0;
    32 }
    33 
    34 int isPrime( int sum)
    35 {
    36     int i,m;
    37     if( sum<2 ) return 0;
    38     if( sum==2 || sum ==3 )return 1;
    39     m =(int) sqrt(sum)+1;
    40     for( i=2; i<m; i++)
    41         if( sum%i==0 )
    42             return 0;
    43     return 1;
    44 }
    在这个国度中,必须不停地奔跑,才能使你保持在原地。如果想要寻求突破,就要以两倍现在速度奔跑!
  • 相关阅读:
    asp.net中分页与存储过程的一些总结
    Ajax与Json的一些总结
    aspx页面与服务器控件间运行原理
    ASP.NET中Server对象的几个方法
    Cookie与Session的一些总结
    ASP.NET的学习之asp.net整体运行机制
    Find offset of first/last found substring
    由于DNS反向解析导致的登录BI启动版速度变慢问题
    4-自定义BI启动版Logon界面
    3-自定义BI启动版是否隐藏CMS名称
  • 原文地址:https://www.cnblogs.com/yuxiaoba/p/8554872.html
Copyright © 2011-2022 走看看