liedou @ 2023-04-09 21:59:12
最后三个点TLE,请问怎么优化???
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
bool isPrime(int n) {
if (n <= 1)
return false;
for (int i = 2; i <= sqrt(n); i++) {
if (n % i == 0)
return false;
}
return true;
}
bool isPalindrome(int d) {
string s = to_string(d);
int left = 0, right = s.size() - 1;
while (left < right) if (s[left++] != s[right--]) return false;
return true;
}
int main() {
int a, b;
cin >> a >> b;
for (; a <= b; a++) if (isPrime(a) && isPalindrome(a)) cout << a << endl;
return 0;
}
by SkyWave @ 2023-04-09 22:02:48
@liedou 先判是不是回文再判是不是质数快很多