Files
python/TangDou/XiTi/1145.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

42 lines
1017 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}