-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path866.回文素数.cpp
72 lines (69 loc) · 1.27 KB
/
866.回文素数.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <string>
using namespace std;
/*
* @lc app=leetcode.cn id=866 lang=cpp
*
* [866] 回文素数
*/
// @lc code=start
class Solution {
private:
bool isPrime(int N) {
int r = sqrt(N);
for (int i = 2; i <= r; i++) {
if (N % i == 0) return false;
}
return true;
}
int get_num_len(int N) {
int ans = 1;
while (N >= 10) {
N /= 10;
ans++;
}
return ans;
}
int reverse_num(int N) {
int ans = 0;
while (N > 0) {
ans = (10 * ans) + (N % 10);
N /= 10;
}
return ans;
}
bool isPrimePalindrome(int N) {
if (!isPrime(N)) return false; // 排除非素数
int reversed_num = reverse_num(N);
if (reversed_num == N) return true; // 数字倒置前后相等则为回文数
return false;
}
public:
int primePalindrome(int N) {
if (N == 1)
return 2;
else if (N <= 3) {
return N;
} else if (N <= 5)
return 5;
else if (N <= 7)
return 7;
else if (N <= 11)
return 11;
while (N) {
int len = get_num_len(N);
if (len % 2 == 0) {
N = 1;
while (len--) {
N *= 10;
}
continue;
}
if (isPrimePalindrome(N)) {
return N;
}
N++;
}
return 0;
}
};
// @lc code=end