zoukankan      html  css  js  c++  java
  • POJ 2262 Goldbach's Conjecture (打表)

    题目链接:

    https://cn.vjudge.net/problem/POJ-2262

    题目描述:

    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
     1 /*
     2 题意描述 
     3 将一个数分成两个奇素数之和的形式,即c=a+b,如果有多种方案,输出b-a差值最大的
     4 
     5 解题思路
     6 打表,枚举查询 
     7 */ 
     8 #include<cstdio>
     9 #include<cmath>
    10 #include<cstring>
    11 const int maxn=1000000+10;
    12 bool isprim[maxn];
    13 void makeprim(int n);
    14 
    15 int main()
    16 {
    17     makeprim(maxn);
    18     int n,i;
    19     //freopen("E:\testin.txt","r",stdin);
    20     while(scanf("%d",&n) == 1 && n != 0){
    21         for(i=3;i<=maxn;i++){
    22             if(i&1 && isprim[i] && (n-i)&1 &&isprim[n-i]){
    23                 printf("%d = %d + %d
    ",n,i,n-i);
    24                 break;
    25             }
    26         }
    27         if(i == maxn+1)
    28             printf("Goldbach's conjecture is wrong.
    ");
    29     }
    30     return 0;
    31 } 
    32 
    33 void makeprim(int n){
    34     memset(isprim,1,sizeof(isprim));
    35     isprim[0]=isprim[1]=0;
    36     int k=(int)sqrt(n+0.5);
    37     for(int i=2;i<=k;i++){
    38         if(isprim[i]){
    39             for(int j=i*2;j<=n;j+=i)
    40                 isprim[j]=0;
    41         }
    42     }
    43 }
  • 相关阅读:
    一口气说出9种分布式ID生成方式,面试官有点懵
    13个Mongodb GUI可视化管理工具,总有一款适合你
    基于mysql-8.0.16-winx64的主从搭建(Windows10系统)
    Windows10安装多个版本的PostgreSQL数据库,但是均没有自动注册Windows服务的解决方法
    Win10安装多个MySQL实例
    内核编译步骤及模块管理
    进程管理类命令
    进程监控类命令
    进程概念介绍
    文件打包压缩
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/9337956.html
Copyright © 2011-2022 走看看