题解 | #查找组成一个偶数最接近的两个素数#
查找组成一个偶数最接近的两个素数
https://www.nowcoder.com/practice/f8538f9ae3f1484fb137789dec6eedb9
#include <iostream>
using namespace std;
bool isPrime(int x)
{
if(x<2)
return false;
for(int i = 2;i*i<=x;i++)
{
if(x%i==0)
return false;
}
return true;
}
int main() {
int n;cin>>n;
int temp = 0;
for(;;temp++)
{
if(isPrime(n/2-temp)&&isPrime(n/2+temp))
break;
}
cout<<n/2-temp<<endl<<n/2+temp;
}
// 64 位输出请用 printf("%lld")
只要从n/2 向两侧等距搜索,两侧的数都是素数即是最接近的两个素数
查看28道真题和解析
联想公司福利 1517人发布