joj-Before 2012

本文探讨了Goldbach猜想,并通过编程实现了一种算法来计算6到2012之间所有偶数的最大质数差(MDP),展示了具体的实现步骤与排序逻辑。

 joj-Before 2012

Description

As we know, 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.

There are various decompose methods for a certain number. The maximal difference of these two prime numbers is called MDP. For example, 20 = 3+17 = 7+ 13, both of the equation satisfies the Goldbach rule. Here the maximal difference is 17 – 3 =14, so the MDP of 20 is 14.

Your task is to calculate all the MDP of even number between 6 and 2012 inclusive. Output these numbers and their MDP order by MDP in ascending sort order. If the MDP is same, order by their value.

Input

This problem has no input.

Output

You should output the even number and it’s MDP in one line (separated by a space) order by the condition above.

Sample Output

6 0
……
100 94
……
2012 1986
……

Hint:Not all the numbers are listed in the sample. There are just some examples. The ellipsis expresses what you should calculate.


题目很好理解,关键是要注意题目要求的是6~2012的even number(偶数),我就是没看清题目,被坑在这点上。。。。。。

#include<stdio.h>
int isPrime(int n)//判断是否为素数
{
    int i,flag=1;
    for(i=2;i<n;i++)
        if(n%i==0)
        {
            flag=0;
            break;
        }
    return flag;
}
int main()
{
    int a[2012],b[2012];//a用来储存这个数字,b用来储存这个数字的MDP
    int i,j,temp,n=0,m=0;
    for(i=6;i<=2012;i+=2)//i要记得每次都要+2,而且这样写的好处就是,这样顺便就把这些结果从小到大排了一遍
        for(j=2;j<=i/2;j++)
            if(isPrime(j)&&isPrime(i-j))
            {
                a[n++]=i;
                b[m++]=i-j-j;
                break;
            }
    for(i=0;i<n-1;i++)
        for(j=i;j<n-1;j++)
            if(b[j]>b[j+1])//按照题目要求,根据MDP排序,本人较水,所以用了一个冒泡排序
            {
                temp=b[j+1];
                b[j+1]=b[j];
                b[j]=temp;
                temp=a[j+1];
                a[j+1]=a[j];
                a[j]=temp;
            }
    for(i=0;i<n;i++)
        printf("%d %d\n",a[i],b[i]);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值