POJ 2262 Goldbach's Conjecture
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 using namespace std; 5 #define N 1000005 6 7 bool prime[N];//int会超内存 8 9 void dabiao()//筛法求素数 10 { 11 int i,j; 12 prime[0]=prime[1]=1; 13 14 for (i=2;i<N;i++) 15 { 16 if (!prime[i]) 17 { 18 for (j=2*i;j<N;j+=i) 19 { 20 prime[j]=1; 21 } 22 } 23 } 24 } 25 26 void test() 27 { 28 int i; 29 for (i=0;i<N;i++) 30 { 31 printf("%2d",prime[i]); 32 } 33 } 34 35 int main() 36 { 37 int n,i; 38 dabiao(); 39 // test(); 40 41 while (scanf("%d",&n)&&n) 42 { 43 for (i=1;i<=n/2;i++)//到n/2即可 44 { 45 if (!prime[i]&&!prime[n-i]) 46 { 47 printf("%d = %d + %d\n",n,i,n-i); 48 break; 49 } 50 } 51 } 52 53 return 0; 54 }