题解 | #回文素数#
回文素数
https://www.nowcoder.com/practice/4802faa9afb54e458b93ed372e180f5c
#include <iostream> using namespace std; const int maxn = 1e4 + 5; int prime[maxn]; int num = 0; bool p[maxn] = {false}; // 判断是否为回文数 bool is_palindromes(int n) { int ans = 0, m = n; while (m > 0) { ans = ans * 10 + m % 10; m /= 10; } return ans == n; } void Find_Prime(int L, int R) { //查找2-R的数里的素数 for (int i = 2; i <= R ; i++) { if (p[i] == false) { prime[num++] = i; for (int j = i + i; j < R + 5 ; j += i) { p[j] = true; } } } int start = 0; //确定L的位置 for (int i = 0 ; i < num ; i ++) { if (prime[i] >= L) { start = i; break; } } //开始统计回文数的个数 int sum = 0; for (int i = start; i < num ; i++) { if(is_palindromes(prime[i])){ sum++; } } cout<< sum <<endl; } int main() { int a, b; while (cin >> a >> b) { Find_Prime(a,b); } }