社区讨论
回文质数88分,SOS!!!
P1217[USACO1.5] 回文质数 Prime Palindromes参与者 1已保存回复 1
讨论操作
快速查看讨论及其快照的属性,并进行相关操作。
- 当前回复
- 1 条
- 当前快照
- 1 份
- 快照标识符
- @mi5hlslt
- 此快照首次捕获于
- 2025/11/19 12:13 4 个月前
- 此快照最后确认于
- 2025/11/19 12:13 4 个月前
CPP
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int n, m;
bool hw(int);//回文
bool ss(int);//素数
int main()
{
int i;
scanf("%d%d", &n, &m);
if (n % 2 == 0) n += 1;
if (m % 2 == 0) m -= 1;
for (i = n;i <= m;i += 2) {
if (hw(i) && ss(i)) printf("%d\n", i);
}
return 0;
}
bool hw(int a)
{
int x = 0, y;
char str[15] = {} ;
while (a != 0) {
str[x] = char(a % 10 + 48);
a = a / 10;
x++;
}
y = strlen(str)-1;
x = 0;
while (x <= y) {
if (str[x] == str[y]) {
x++;
y--;
}
else return false;
}
return true;
}
bool ss(int a)
{
int i;
for (i = 2;i <= trunc(sqrt(a));i++) {
if (a%i == 0) return false;
}
return true;
}
回复
共 1 条回复,欢迎继续交流。
正在加载回复...