回文,汉语词语,指汉语中的回文语法,即把相同的词汇或句子,在下文中调换位置或颠倒过来,产生首尾回环的情况,叫做回文,也叫回环。
以下是回文的代码。
输入N输出1~N之间的回文。
#include <cstdio>
#include <iostream>
using namespace std;
bool huiwen(int n){
int t=n, sum=0, s;
while(n!=0){
s=n%10;
sum = sum*10+s;
n = n/10;
}
if(sum==t){
return 1;
}
else{
return 0;
}
}
int main(){
int a, cnt=0;
bool torf;
scanf("%d", &a);
for(int i=1; i<=a; i++){
torf = huiwen(i);
if(torf){
cnt++;
}
}
printf("%d", cnt);
return 0;
}
质数是指在大于1的自然数中,除了1和它本身以外不再有其他因数的自然数。
一下是输出质数的代码。
输入N输出1~N之间的回文。
#include<iostream>
using namespace std;
int zhishu(int n){
int i;
if(n<2)
return 0;
for(i=2;i*i<=n;i++)
if(n%i==0)
return 0;
}
int main(){
int n,a;
cin>>n;
while(n>0){
cin>>a;
if(zhishu(a))
cout<<a<<" ";
n--;
}
return 0;
}
那我们将它组成到一起,便是回文质数。
#include<iostream>
using namespace std;
int zhishu(int x){
if (x<2){
return 0;
}
for(int i=2;i<x;i++){
if(x%i==0){
return 0;
}
}
return x;
}
int huiwen(int temp){
if(temp==0){
return 0;
}
int x = 0;
int s = temp;
while(s>0){
x = x*10+s%10;
s = s/10;
}
if(x==temp){
cout<<temp<<endl;
}
else{
return 0;
}
}
int main(){
int n,m,temp;
cin>>n>>m;
for(int i=n;i<=m;i++){
temp=zhishu(i);
huiwen(temp);
}
return 0;
}