题目描述:
代码示例:
#include <bits/stdc++.h>
using namespace std; int main(){
// 算法思想1:遍历小于该偶数的所有素数,存入数组中,遍历数组找出两个数之和等于偶数的数int n;cout << "输入样例" << endl;cin >> n;int nums[n];int k = 0;for(int i = 2; i <= n; i++){bool tag = true;for(int j = 2; j <= sqrt(i); j++){if(i % j == 0){tag = false;break;}}if(tag == true){nums[k++] = i;}} // for(int i = 0; i < k; i++){
// cout << nums[i] << " ";
// }//测试素数结果cout << "输出样例" << endl;for(int i = 0; i < k; i++){for(int j = i; j < k; j++){if(nums[i] + nums[j] == n){cout << nums[i] << " " << nums[j] << " " << endl;}}} return 0;
}
运行结果: