You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1017 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
//判断是不是质数
bool isPrime(int n) { //最快的方法
if (n < 2) return false;
if (n == 2 || n == 3) return true;
if (n % 6 != 1 && n % 6 != 5) return false;
for (int i = 5; i <= floor(sqrt(n)); i += 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
int main() {
int n;
cin >> n;
//1、找到所有比当前数小的质数
int a[100] = {0};
int k = 0;
for (int i = 2; i <= n; ++i) {
if (isPrime(i)) {
a[k++] = i;
}
}
//2、两两配对是不是加在一起等于当前数
bool found = false;
for (int i = 0; i < k; ++i) {
for (int j = 1; j <= k; ++j) {
if (a[i] + a[j] == n) {
cout << n << "=" << a[i] << "+" << a[j] << endl;
found = true;
break;
}
}
if (found) break;
}
return 0;
}