OJ or prompt time error ah, please tell me how to save time?
OJ indicates that the time is out of limit, can you help me change it?
Description
Because 151 is both a prime number and a palindrome number(it looks the same from left to right as from right to left), 151 is a palindrome prime Href = "https://ask.csdn.net/5%3C=a%3Cb%3C=100, 000000" target = "_blank" > < span > a, b < / span > < / a > all palindrome between prime Numbers;
Enter
Line 1: Two integers a and b.
Output
Output a list of palindrome primes, one in line.
Sample input
5 500 < br / > < br / > the sample output 5 7 < br / > < br / > < br / > 101 < br / > < br / > < br / > 151 131 181 191 < br / > < br / > < br / > < br / > 353 313 373 383
#include <bits/stdc++.h>
using namespace std;
bool isSymm(int a) {
int j, k=0;
j = a;
while (j > 0) {
k = k*10+ j % 10 ;
j /= 10;
}
if (k == a)return true;
else return false;
}
int main()
{
int a,b,i=2;
cin>>a>>b;
for(;a<=b;a++){
while(a%i!=0 && i<=a-1)
{
i++;
}
if (i>a-1 && isSymm(a))
{
cout<<a<<endl;
}
i=2;
}
return 0;
}
0 Answer
#include
using namespace std;
// 判断是否为回文数
bool isSymm(int a) {
int j = a, k = 0;
while (j > 0) {
k = k * 10 + j % 10;
j /= 10;
}
return k == a;
}
// 判断是否为质数
bool isPrime(int a) {
if (a == 1) return false;
if (a == 2) return true;
for (int i = 2; i <= sqrt(a); i++) {
if (a % i == 0) {
return false;
}
}
return true;
}
int main() {
int a, b;
cin >> a >> b;
for (int i = a; i <= b; i++) {
if (isSymm(i) && isPrime(i)) {
cout << i << endl;
}
}
return 0;
}
这家伙很懒,什么都没留下...