PAT 甲级 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 ;
}
PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))的更多相关文章
- PAT-1015 Reversible Primes (20 分) 进制转换+质数
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
- PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642
PAT (Advanced Level) Practice 1015 Reversible Primes (20 分) 凌宸1642 题目描述: A reversible prime in any n ...
- PAT 1015 Reversible Primes[求d进制下的逆][简单]
1015 Reversible Primes (20)(20 分)提问 A reversible prime in any number system is a prime whose "r ...
- PAT 甲级 1015 Reversible Primes(20)
1015 Reversible Primes(20 分) A reversible prime in any number system is a prime whose "reverse& ...
- PAT Advanced 1015 Reversible Primes (20) [素数]
题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...
- PAT 1015 Reversible Primes (20分) 谜一般的题目,不就是个进制转换+素数判断
题目 A reversible prime in any number system is a prime whose "reverse" in that number syste ...
- 【PAT甲级】1015 Reversible Primes (20 分)
题意: 每次输入两个正整数N,D直到N是负数停止输入(N<1e5,1<D<=10),如果N是一个素数并且将N转化为D进制后逆转再转化为十进制后依然是个素数的话输出Yes,否则输出No ...
- PAT (Advanced Level) Practice 1015 Reversible Primes (20 分)
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
- 1015 Reversible Primes (20 分)
A reversible prime in any number system is a prime whose "reverse" in that number system i ...
随机推荐
- canvas制图学习
<!DOCTYPE html> <html lang="zh-en"> <head> <meta charset="UTF-8& ...
- python_公共方法
1.计算长度 value = "wangdianchao" # 计算字符个数(长度) number = len(value) print(number) 2.索引取值 value ...
- Node.js Websocket 区分不同的用户
实现ws://serverIP:port/:param1/:param2 .通过param1,param2来管理不同的ws回话,以便实现群发和指定用户的消息推送 npm install ws --sa ...
- Web UI开发速速种草—Kendo UI for jQuery网格编辑操作概述
Kendo UI for jQuery最新试用版下载 Kendo UI目前最新提供Kendo UI for jQuery.Kendo UI for Angular.Kendo UI Support f ...
- DevExpress Winforms使用大揭秘!那些你不了解的SvgImageBox控件
DevExpress Winforms Controls 内置140多个UI控件和库,完美构建流畅.美观且易于使用的应用程序.无论是Office风格的界面,还是分析处理大批量的业务数据,DevExpr ...
- Ubuntu 搭建 Ftp 服务器
1.在 ubuntu 中 下载 vsftpd 要是你安装了 vsftpd 输入:vsftpd -v ,会有版本提示 如果没有,则进行安装 vsftpd 输入 apt-get install vs ...
- 一款超好用的第三方评论插件--Gittalk
使用GITALK的背景: 1. 最近在做一个基于Java的个人博客系统,已经基本完工了,突然发现怎么没有评论的操作,如果再从头开始从数据库开始写的话,花费的代价有点大,于是乎我就在网上寻找一款适合我的 ...
- FireFox版本大于36.0,Yslow不能正常使用解决方案
Yslow暂时不支持firefox 36.0及以上版本你可以使用它的书签版本.访问这里 http://yslow.org/mobile/把页面最后的那个 Desktop Bookmarklet Ins ...
- angularjs 动态计算平均值
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Math.cbrt() Math.sqrt() Math.pow()
Math.pow() 能实现 Math.cbrt() 和 Math.sqrt() 的功能,但并不完全相同. 1. Math.pow()和Math.cbrt()的区别 function isCube(m ...