题解 | #查找组成一个偶数最接近的两个素数#
查找组成一个偶数最接近的两个素数
https://www.nowcoder.com/practice/f8538f9ae3f1484fb137789dec6eedb9
#include <iostream> #include <cmath> using namespace std; // 判断是否素数 bool isSu(int x){ for(int i=2;i<=(int)sqrt(x);++i){ if(x%i==0) return false; } return true; } //若素数a+b=n //则(a+b)/2=n/2 //可以从n/2向左右寻找素数 int main() { int n; cin >> n; int i=n/2; int j=n/2; while(i>=2){ if(isSu(i)&&isSu(j)){ cout << i << endl << j; break; } --i;++j; } } // 64 位输出请用 printf("%lld")