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;
    }
  • 相关阅读:
    让UILabel具有链接功能,点击后调用safari打开网址
    自定义UITableViewCell上的delete按钮
    iOS7 SDK各种坑——手Q团队总结
    字符串转成NSDate类型,计算与当前时间的相差,年数,天数,时分秒
    AFNetworking 2.0 Migration Guide
    UIView Class Reference
    AFNetworking实现程序重新启动时的断点续传
    CI框架下JS/CSS文件路径的设置
    CI session的使用
    php调用QQ登录(第三方登录)
  • 原文地址:https://www.cnblogs.com/chenchen-12/p/10142721.html
Copyright © 2011-2022 走看看