P1304 Goldbach's Conjecture

Foreword

This is a red question

Face questions

Title Description

Enter an even number  N (N <= 10000) N ( N < = . 1 0 0 0 0 ), verification 4 ~ N Meets all even Goldbach conjecture: Any even number greater than 2 can be written as the sum of two primes. If a number of points more than one method, the output of the first addend minimal compared to other sub-programs method. E.g. 10, 10 = 3 + 5 + 7 = 5, 5 + 5 = 10 is the wrong answer.

Input Format

The first row N

Output Format

2 + 2 4 = 6 = 3 + 3 ...... N = x + y

Sample input and output

Input # 1

 

10

 

Output # 1

 

4=2+2
6=3+3
8=3+5
10=3+7

 

Topic analysis

It is a relatively simple screening method, but after screening practices varied, but in different time complexity of it.

Method 1: After screening the result obtained by subtracting the two numbers

Method 2: After screening the prime could add determination result obtained

。。。。。。

AC code is as follows

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int prime(int x)
 4 {
 5     for(int i=2;i<=sqrt(x);i++)
 6     {
 7         if(x%i==0)
 8         {
 9             return 0;
10         }
11     }
12     return 1;
13 } 
14 int main()
15 {
16     int n;
17     cin>>n;
18     for(int i=4;i<=n;i+=2)
19     {
20         for(int j=2;j<=i/2;j++)
21          if(prime(j)==1)
22          {
23                if(prime(i-j)==1)
24              {
25                  cout<<i<<"="<<j<<"+"<<i-j<<endl;
26                  break;
27              }
28          }
29     }
30     return 0;
31 } 

 

Guess you like

Origin www.cnblogs.com/Michael666/p/11626914.html