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

题目大意:

一开始理解错了题意。。。。
数字N在D进制下是不是双重素数。双重素数是本身和倒数皆为素数的数。

实现:判断N是否为素数。如果不是,输出No,否则将该数在D进制下倒过来再化为十进制数,判断是否为素数。如果是,输出Yes,否则输出No.

复习判断素数知识点 注意 ‘ = ’ !!!

#include<bits/stdc++.h>
using namespace std;
bool prime(int x){
if(x==||x==){
return false;
}
if(x==){
return true;
}
for(int i=;i<=sqrt(x);i++){//这个地方忘记了=号!!!
if(x%i==){
return false;
}
}
return true;
}
int main()
{
int a;
int d;
while(cin>>a)
{
if(a<){
break;
}
cin>>d;
//先判断本身是不是素数
if(!prime(a)){
cout<<"No"<<endl;
continue;
}
//根据相应地进制转
string s="";
int x;
while(a){
s+=char(a%d+'');
a=a/d;
}
//cout<<s<<endl; //反向再把它从d进制转成10进制
int l = s.length();
x=;
for(int i=;i<l;i++){
x=x*d+s[i]-'';
}
//cout<<x<<endl;
if(prime(x)){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
}
return ;
}
 

最新文章

  1. java线程 - 多线程 - 守护线程
  2. xsl-mode属性用法
  3. VS2015链接错误一则
  4. 用VisualC++建立SOAP客户端应用(一)
  5. iso socket基础2
  6. 转载---linux运维相关
  7. SQLite 入门教程(一)基本控制台(终端)命令
  8. 用Firefly创建第一个工程
  9. PC-lint 简明教程
  10. html5橡皮檫特效
  11. Mac OS X 安装后的简单设置
  12. Ubuntu-修改图片分辨率
  13. 一个两年java程序猿的2017个人总结
  14. CentOS5 可用yum源
  15. .Net—反射
  16. Chapter 5 Blood Type——14
  17. SQL Server2016安装
  18. 防止enter提交表单
  19. request和reponse
  20. vue中子组件通过$parent操作父组件的参数或者方法

热门文章

  1. SVN版本控制—branches、trunk、tag篇
  2. js面向对象篇(一)
  3. less混合
  4. Sonya and Exhibition
  5. Nginx入门(一)——安装和配置
  6. Nginx中ngx_stream_core_module和ngx_stream_proxy_module
  7. php 的一个异常处理程序
  8. springboot2.0入门(六)-- ymal语法、数据校验
  9. JQuery 实践--让页面动起来
  10. Mysql 高可用集群PXC