新手村 Boss战-入门综合练习1 哥德巴赫猜想(升级版)

题目链接

题意理解

这就是随便写的,10分钟肯定能写完。

代码

#include <cstring>
#include <iostream>
#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <iomanip>
#include <vector>

using namespace std;

/*
* 素数筛选,判断小于MAXN的数是不是素数。
* notprime是一张表,为false表示是素数,true表示不是素数
*/
const int MAXN = 20010;
bool notprime[MAXN]; //值为false表示素数,值为true表示非素数

void init() {
    memset(notprime, false, sizeof notprime);
    notprime[0] = notprime[1] = true;
    for(int i = 2;i < MAXN; i++) {
        if(!notprime[i]) {
            if(i > MAXN/i) {
                continue; //防止后面i*i溢出(或者i,j用long long)
            }
            //直接从i*i开始就可以,小于i倍的已经筛选过了,注意是j+=i
            for(int j = i * i;j < MAXN;j += i) {
                notprime[j]=true;
            }
        }
    }
}

void read(int &x) {
    x = 0;
    int f = 1;
    char ch = getchar();
    while(ch > '9'||ch < '0') {
        if(ch == '-') {
            f = -1;
        }
        ch = getchar();
    }
    while(ch >= '0' && ch <= '9') {
        x = x * 10 + (int)(ch - 48);
        ch = getchar();
    }
    x = x * f;
}



int main() {
    int n;
    read(n);
    init();
    for(int i = 2; i < n; i++) {
        if(notprime[i]) {
            continue;
        }
        for(int j = 2; j < n; j++) {
            if(notprime[j]) {
                continue;
            }
            if(i + j >= n) {
                break;
            }
            if(!notprime[n - i - j]) {
                printf("%d %d %d", i, j, n - i - j);
                return 0;
            }
        }
    }
    return 0;
}

欢迎加入“不会算法一群菜鸟”,群号是⑥⑥①⑨②2025,这是我设置的一道很低的门槛用来阻止广告的。入群的验证暗号是:我爱编译原理

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值