题解 | #查找组成一个偶数最接近的两个素数#
查找组成一个偶数最接近的两个素数
http://www.nowcoder.com/questionTerminal/f8538f9ae3f1484fb137789dec6eedb9
#include <utility>
#include <algorithm>
#include <climits>
#include <cmath>
using namespace std;
bool istrue(int number){
if (number <= 1) return false;
for (int i = 2; i < number; ++i){
if (number % i == 0){
return false;
}
}
return true;
}
int main(){
int n = 0;
while (cin >> n){
int first = 0, second = 0, delta = 100000001;
for (int i = n / 2; i >= 2; --i){
int j = n - i;
// cout << istrue(i) << " " << istrue(j) << endl;
// cout << i << " " << j << endl;
if (istrue(i) && istrue(j) && delta > j - i){
first = i, second = j;
delta = j - i;
}
}
cout << first << endl << second << endl;
}
return 0;
}