描述 任意一个偶数(大于2)都可以由2个素数组成,组成偶数的2个素数有很多种情况,本题目要求输出组成指定偶数的两个素数差值最小的素数对 请实现如下接口 public static class PrimePair { public int primeMin; public int primeMax; } public static PrimePair findPrimeNumber(int number) { /* 请实现 */
return null; }譬如:输入20 ,输出 7 13 约束 number为输入的偶数,5 < inum <= 10000
知识点 循环 运行时间限制 10M 内存限制 128 输入 输入一个偶数 输出 输出两个素数 样例输入 20 样例输出 7 13
#include <iostream> #include<vector> #include<algorithm> #include<string> using namespace std; bool is_pri(int n){ if (n < 3)return false; for (int i = 2; i <= n / 2; i++){ if ((n%i) == 0) return false; } return true; } int main(){ int n; cin >> n; int a; int b; int min = n; for (int i = 3; i < n / 2; i += 2){ if (is_pri(i) && is_pri(n - i)){ if ((n - 2 * i) < min){ a = i; b = n - i; } } } cout << a <<endl<< b << endl; return 0; }