• poj 2262【素数表的应用---判断素数】【哈希】


    Goldbach's Conjecture
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 35214   Accepted: 13493

    Description

    In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture: 
    Every even number greater than 4 can be 
    written as the sum of two odd prime numbers.

    For example: 
    8 = 3 + 5. Both 3 and 5 are odd prime numbers. 
    20 = 3 + 17 = 7 + 13. 
    42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23.

    Today it is still unproven whether the conjecture is right. (Oh wait, I have the proof of course, but it is too long to write it on the margin of this page.) 
    Anyway, your task is now to verify Goldbach's conjecture for all even numbers less than a million. 

    Input

    The input will contain one or more test cases. 
    Each test case consists of one even integer n with 6 <= n < 1000000. 
    Input will be terminated by a value of 0 for n.

    Output

    For each test case, print one line of the form n = a + b, where a and b are odd primes. Numbers and operators should be separated by exactly one blank like in the sample output below. If there is more than one pair of odd primes adding up to n, choose the pair where the difference b - a is maximized. If there is no such pair, print a line saying "Goldbach's conjecture is wrong."

    Sample Input

    8
    20
    42
    0
    

    Sample Output

    8 = 3 + 5
    20 = 3 + 17
    42 = 5 + 37
    题目大意:给出一个6-1000000的偶整数n,判断是不是两个素数的和,如果是,按照要求的格式输出这两个素数,如果不是或者是n是奇数,输出“Goldbach's conjecture is wrong.”;输入0时退出程序。
    这道题需要用到素数表,不过不用打印,只需要标记下谁是素数即可,所以只需要将打印素数表的代码稍微一修改就可以了。
     1 #include<iostream>
     2 #include<math.h>
     3 using namespace std;
     4 int prime[1001000]={0};
     5 int top=0;
     6 void print_prime()
     7 {
     8     int i;
     9     double x=sqrt(1001000.0);
    10     int n=int(x);
    11     //cout<<sqrt(MAXN)<<endl;
    12     //cout<<n<<endl;
    13     for( i=2; i<n; i++)
    14     {
    15         if(prime[i]==0)
    16         {
    17             for(int j = i*i; j<1001000; j+=i)
    18                 prime[j]=1;
    19            // prime[++top]=i;
    20         }
    21     }
    22     /*for(i=n; i<MAXN; i++)
    23         if(prime[i]==0)
    24             prime[++top]=i;*/
    25 }
    26 int main()
    27 {
    28     print_prime();
    29     int n;
    30     while(1)
    31     {
    32         cin>>n;
    33         if(n==0)break;
    34         if(n%2==1)
    35         {
    36             cout<<"Goldbach's conjecture is wrong."<<endl;
    37             continue;
    38         }
    39         bool flag=true;
    40         int i;
    41         for(i=3;i<=n/2;i=i+2)
    42         {
    43             if(prime[i]==0&&prime[n-i]==0)
    44             {
    45                 cout<<n<<" = "<<i<<" + "<<n-i<<endl;
    46                 flag=false;
    47                 break;
    48             }
    49         }
    50         if(flag==true)cout<<"Goldbach's conjecture is wrong."<<endl;
    51     }
    52     return 0;
    53 }
    View Code
  • 相关阅读:
    python-opencv-绘图函数
    sublime下package control安装无效解决
    关于vue.js中slot的理解
    关于vue.js父子组件数据传递
    oAuth2.0及jwt介绍
    nodejs文件上传组件multer使用
    nodejs设置服务端允许跨域
    添加swagger api文档到node服务
    关于for循环里面异步操作的问题
    nodejs记录2——一行代码实现文件下载
  • 原文地址:https://www.cnblogs.com/kuangdaoyizhimei/p/3439782.html
Copyright © 2020-2023  润新知