zoukankan      html  css  js  c++  java
  • 问题 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;
    }
  • 相关阅读:
    PHP.29-TP框架商城应用实例-后台6-商品会员添加-价格、级别
    PHP.TP框架下商品项目的优化4-优化商品添加表单js
    TP-常见错误1
    python+pyqt5实现24点小游戏
    OpenCV+Python识别车牌和字符分割的实现
    python爬取酷狗音乐排行榜
    python-itchat 统计微信群、好友数量的实例
    Python爬虫beautifulsoup4常用的解析方法总结
    Python视频人脸检测识别
    Python openpyxl : Excel 文档简单操作
  • 原文地址:https://www.cnblogs.com/chenchen-12/p/10142721.html
Copyright © 2011-2022 走看看