题解 | #查找组成一个偶数最接近的两个素数#

查找组成一个偶数最接近的两个素数

https://www.nowcoder.com/practice/f8538f9ae3f1484fb137789dec6eedb9

#include <cmath>
#include <iostream>
using namespace std;

bool is_prime(int nums)
{
    for(int i = 2; i <= (int)sqrt(nums); ++i)
        if(nums % i == 0)   return false;
    return true;
}

int main() {
    int a;
    while (cin >> a) { // 注意 while 处理多个 case
        //对半开始往左和右移动,是差值最小的
        int left = a/2, rigth = a/2;
        while(!(is_prime(left) && is_prime(rigth)))
            ++rigth,--left;
        cout << left << "\n" << rigth << endl;
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

星辰再现:裁员给校招生腾地方
点赞 评论 收藏
分享
不亏是提前批,神仙打架,鼠鼠不配了
站队站对牛:现在92都报工艺岗了
投递韶音科技等公司7个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务