题解 | #查找组成一个偶数最接近的两个素数#
查找组成一个偶数最接近的两个素数
https://www.nowcoder.com/practice/f8538f9ae3f1484fb137789dec6eedb9
#include <cmath>
#include <iostream>
using namespace std;
bool is_prime(int nums)
{
for(int i = 2; i <= (int)sqrt(nums); ++i)
if(nums % i == 0) return false;
return true;
}
int main() {
int a;
while (cin >> a) { // 注意 while 处理多个 case
//对半开始往左和右移动,是差值最小的
int left = a/2, rigth = a/2;
while(!(is_prime(left) && is_prime(rigth)))
++rigth,--left;
cout << left << "\n" << rigth << endl;
}
return 0;
}
// 64 位输出请用 printf("%lld")
华为HUAWEI工作强度 1304人发布

