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

Miracle_InDream @ 2024-01-22 21:54:45

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

by Hagasei @ 2024-01-22 21:59:46

@shooting__star ss() 函数只需循环至 \sqrt n


by a1co0av5ce5az1cz0ap_ @ 2024-01-22 22:00:18

for(int i=2;i<=n/2+1;i++)

n 改成 a


by Miracle_InDream @ 2024-01-22 22:10:15

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

额……还是没输出()


by furina_superstar @ 2024-01-22 22:14:19

怎么感觉素数这事似曾相识呢,我是不是上次和你说过()


by Miracle_InDream @ 2024-01-22 22:15:19

@fu__men 啊这


by NC20061226 @ 2024-01-22 22:17:01

众所周知,打表是个不戳的算法


by Miracle_InDream @ 2024-01-22 22:17:36

@NC20061226 哦所以呢()


by a1co0av5ce5az1cz0ap_ @ 2024-01-22 22:17:37

@shooting__star flag 在函数里要先赋值为 1


by Miracle_InDream @ 2024-01-22 22:18:26

AC。此帖结。


|