forked from newde/homework
新增 20260321
This commit is contained in:
45
20260321
Normal file
45
20260321
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
#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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user