Desktop上 c++ 这个程序为啥不能出来结果?
int main()
{
int n,m,s = 0,i,q;
cin>>n;
q=n;
while(q!=0){i=1;
i++;q=n%i;
}if(q=0){cout<<"不是回文质数";
//TODO
}
else{m = n;
while(n)
{
s *= 10;
s += n%10;
n /= 10;
}
if(s == m){
cout<<"是回文质数";}
}
return 0;
}
质数判断有误
if(q==0)
1.首先质数判断有问题
2.逻辑有问题,一个数可以是质数但不是回文质数,而你的代码直接把不是质数当作不是回文质数。
改过之后的代码:
#include<iostream>
using namespace std;
int main(){
int n, m, s = 0, i, q;
cin >> n;
q = n;
for (int i = 2; i < n; i++)
{
if (n%i == 0)
{
cout << "不是质数" << endl;
return 0;
}
}
while (n)
{
s = s * 10 + n % 10;
n = n / 10;
}
if (s == q)
cout << "是回文质数" << endl;
else
cout << "是质数但不是回文质数" << endl;
return 0;
}
如果有用,请采纳哦。