题解
2023-08-17 14:30:47
发布于:广东
1阅读
0回复
0点赞
#include <bits/stdc++.h>
using namespace std;
bool isPrime(int num) {
if (num < 2) {
return false;
}
for (int i = 2; i * i <= num; i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
int main() {
int N;
cin >> N;
vector<bool> isCom(N + 1, false);
vector<int> primes;
for (int i = 2; i <= N; i++) {
if (!isCom[i]) {
primes.push_back(i);
for (int j = i * 2; j <= N; j += i) {
isCom[j] = true;
}
}
}
for (int num = 4; num <= N; num += 2) {
bool found = false;
int prime1 = 0, prime2 = 0;
for (int i = 0; i <= primes.size() && primes[i] <= num / 2; i++) {
int complement = num - primes[i];
if (isPrime(complement)) {
found = true;
prime1 = primes[i];
prime2 = complement;
break;
}
}
if (!found) {
cout << num << " cannot be expressed as the sum of two prime numbers." << endl;
} else {
cout << num << "=" << prime1 << "+" << prime2 << endl;
}
}
return 0;
}
太经典了
这里空空如也
有帮助,赞一个