• 问题 C: Goldbach's Conjecture


    题目描述

    Goldbach's Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2. 
    This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.

    A sequence of even numbers is given as input. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs.

    输入

    An integer is given in each input line. You may assume that each integer is even, and is greater than or equal to 4 and less than 2^15. The end of the input is indicated by a number 0.

    输出

    Each output line should contain an integer number. No other characters should appear in the output.

    样例输入

    4
    10
    16
    0
    

    样例输出

    1
    2
    2

    题意:给出大于等于4的数p 然后找出俩个素数相加为p p1+p2 和 p2+p1 算一种


    这个就是判断 <=n/2 的数 然后访问vis[i]&&vis[n-i] 都是素数 那么就是even number
    #include<bits/stdc++.h>
    
    using namespace std;
    const int N=4e4;
    int prime[N];
    bool vis[N];
    int cnt=0;
    void isprime(int n)
    {
        fill(vis,vis+N,false);
        cnt=0;
        for(int i=2; i<n; i++)
        {
            if(!vis[i])
            {
                prime[cnt++]=i;
            }
            for(int j=i+i; j<n; j+=i)
            {
                vis[j]=true;
            }
        }
    }
    int main()
    {
        int n;
        isprime(N);
        while(scanf("%d",&n)==1,n){
            int sum=0;
            for(int i=2;i<=n/2;i++){
                if(!vis[i]&&!vis[n-i]) sum++;
            }
            printf("%d
    ",sum);
        }
        return 0;
    }

    直接模拟下 也行 但是复杂度是n^2

    #include<bits/stdc++.h>
      
    using namespace std;
    const int N=1e6+10;
    int prime[N];
    bool vis[N];
    int cnt=0;
    void isprime(int n)
    {
        fill(vis,vis+N,false);
        cnt=0;
        for(int i=2; i<n; i++)
        {
            if(!vis[i])
            {
                prime[cnt++]=i;
            }
            for(int j=i+i; j<n; j+=i)
            {
                vis[j]=true;
            }
        }
    }
    int main()
    {
        int n;
        while(scanf("%d",&n)==1,n){
            isprime(n);
            int sum=0;
            int flag=0;
            for(int i=0;i<cnt;i++){
                for(int j=i;j<cnt;j++){
                    if(prime[i]+prime[j]==n){
                        sum++;
                    }
                }
            }
            printf("%d
    ",sum);
        }
        return 0;
    }
  • 相关阅读:
    Nginx Rewrite相关功能-ngx_http_rewrite_module模块指令概述
    Linux操作系统安全-加密和安全扫盲篇
    Nginx安全相关配置-自定义Nginx版本信息
    Nginx高级配置-官网logo之favicon.ico
    Linux操作系统的打包/归档工具介绍
    Nginx高级配置-实现多域名HTTPS
    Nginx高级配置-https功能
    Linux操作系统的压缩、解压缩工具介绍
    Nginx高级配置-压缩功能
    Nginx高级配置-自定义json格式日志
  • 原文地址:https://www.cnblogs.com/chenchen-12/p/10142721.html
Copyright © 2020-2023  润新知