70求助

P1579 哥德巴赫猜想(升级版)

jyz120609 @ 2022-09-02 20:37:04

#include <bits/stdc++.h>
using namespace std;
int ss(int n) {
    if(n==0||n==1) return 0;
    for(int i=2; i<=sqrt(n); i++)
        if(n%i==0) return 0;
    return 1;
}
int main() {
    int n;
    cin>>n;
    for (int i=1; i<n; i++)
        if(ss(i)&&i%2!=0) {
            for (int j=i; j<n; j++)
                if(j%2!=0&&ss(j))
                    if(ss(n-i-j)) {
                        cout<<i<<" "<<j<<" "<<n-i-j;
                        return 0;
                    }
        }
}

by GLNGLN @ 2022-09-03 10:32:54

你的ss函数我没有改,但是我将你主函数优化了一下

代码如下:```c int main() { int n; cin>>n; num=n-4; if(ss(num)==1) { cout<<2<<" "<<2<<" "<<n; return 0; } n=n-3; for(int i=2;i<=n;i++) { if(ss(i)==1&&ss(n-i)==1) { cout<<3<<" "<<i<<" "<<n-i; break; } } return 0; }


by GLNGLN @ 2022-09-03 11:12:05

1.先考虑两个二,(因为和为奇数),若不行,则执行2

2.把3减去,则转化为基础哥德巴赫猜想,就解出来了


by Czq_forever_ @ 2022-10-12 18:14:06

@jyz120609

#include <bits/stdc++.h>
using namespace std;
int ss(int n){
    for(int i=2;i<=sqrt(n);i++)
        if(n%i==0) return false;
    return true;
}
int main() {
    int n;
    cin>>n;
    for(int i=2;i<=n/3;i++)
        if(ss(i)){
            for(int j=i;j<=n/3;j++)
                if(ss(j))
                    if(ss(n-i-j)){
                        cout<<i<<" "<<j<<" "<<n-i-j;
                        return 0;
                    }
        }
    return 0;
}

by Czq_forever_ @ 2022-10-12 18:15:16

@Czqforever

牛逼!!!_


by jyz120609 @ 2022-10-13 12:24:37

@Czqforever Thank you!AC!


|