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==0||x==1){
return false;
}
if(x==2){
return true;
}
for(int i=2;i<=sqrt(x);i++){//这个地方忘记了=号!!!
if(x%i==0){
return false;
}
}
return true;
}
int main()
{
int a;
int d;
while(cin>>a)
{
if(a<0){
break;
}
cin>>d;
//先判断本身是不是素数
if(!prime(a)){
cout<<"No"<<endl;
continue;
}
//根据相应地进制转
string s="";
int x;
while(a){
s+=char(a%d+'0');
a=a/d;
}
//cout<<s<<endl;
//反向再把它从d进制转成10进制
int l = s.length();
x=0;
for(int i=0;i<l;i++){
x=x*d+s[i]-'0';
}
//cout<<x<<endl;
if(prime(x)){
cout<<"Yes"<<endl;
}
else{
cout<<"No"<<endl;
}
}
return 0;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Java 中堆内存和栈内存上的数据分布和特点
· 开发中对象命名的一点思考
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· 本地部署 DeepSeek:小白也能轻松搞定!
· 如何给本地部署的DeepSeek投喂数据,让他更懂你
· 从 Windows Forms 到微服务的经验教训
· 李飞飞的50美金比肩DeepSeek把CEO忽悠瘸了,倒霉的却是程序员
· 超详细,DeepSeek 接入PyCharm实现AI编程!(支持本地部署DeepSeek及官方Dee