joj-Before 2012

来源:互联网 发布:大数据产业园发展模式 编辑:程序博客网 时间:2024/05/29 02:18

 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;}


原创粉丝点击