Files
homework/20260321
2026-03-21 02:16:45 -04:00

45 lines
699 B
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <iostream>
using namespace std;
// 判断是否是素数
bool isPrime(int n)
{
if (n < 2) return false;
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false;
}
return true;
}
// 判断是否是回文数
bool isPalindrome(int n)
{
int original = n;
int reversed = 0;
while (n > 0)
{
reversed = reversed * 10 + n % 10;
n /= 10;
}
return original == reversed;
}
int main()
{
cout << "100到1000之间的回文素数有" << endl;
for (int i = 100; i <= 1000; i++)
{
if (isPrime(i) && isPalindrome(i))
{
cout << i << " ";
}
}
return 0;
}