标签:des style blog http color os io strong
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 37693 | Accepted: 14484 |
Description
Every even number greater than 4 can be
written as the sum of two odd prime numbers.
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.
Input
Output
Sample Input
8 20 42 0
Sample Output
8 = 3 + 5 20 = 3 + 17 42 = 5 + 37
Source
1 #include<cmath> 2 #include<cstdio> 3 #include<cstring> 4 #include<stdlib.h> 5 #include<algorithm> 6 using namespace std; 7 int isprime(int num) 8 { 9 int k=sqrt(num),i; 10 for(i=2;i<=k;i++) 11 { 12 if(num%i==0) 13 break; 14 } 15 if(i>k) 16 return 1; 17 else 18 return 0; 19 } 20 int main() 21 { 22 //freopen("in.txt","r",stdin); 23 int n; 24 while(scanf("%d",&n)&&n) 25 { 26 int flag=0,ans; 27 for(int i=n-2;i>=0;i--) 28 { 29 if(isprime(i)) 30 { 31 ans=n-i; 32 if(isprime(ans)) 33 { 34 flag=1; 35 printf("%d = %d + %d\n",n,ans,i); 36 break; 37 } 38 } 39 } 40 if(flag==0) 41 printf("Goldbach‘s conjecture is wrong.\n"); 42 } 43 return 0; 44 }
不知道为什么,先把素数筛出来再拿出来用会超时
(TLE)
1 #include<cstdio> 2 #include<cmath> 3 #include<cstring> 4 #include<stdlib.h> 5 #include<algorithm> 6 using namespace std; 7 const int MAXN=1000000+10; 8 const int N=999983; 9 int prime[MAXN],vis[MAXN]; 10 int cnt; 11 void init()//素数筛法 12 { 13 int i,j; 14 for(i=2;i<=N;i++) 15 { 16 if(i%2==0) 17 vis[i]=0; 18 else 19 vis[i]=1; 20 } 21 for(i=3;i<=sqrt(N);i+=2) 22 { 23 if(vis[i]) 24 for(j=i+i;j<N;j+=i) 25 vis[j]=0; 26 } 27 cnt=1; 28 prime[0]=2; 29 for(i=2;i<N;i++) 30 if(vis[i]) 31 prime[cnt++]=i; 32 } 33 int main() 34 { 35 //freopen("in.txt","r",stdin); 36 init(); 37 int n; 38 while(scanf("%d",&n)&&n) 39 { 40 int flag=0,ans; 41 for(int i=cnt-1;i>=0;i--) 42 { 43 if(prime[i]<n) 44 { 45 ans=n-prime[i]; 46 if(binary_search(prime,prime+cnt,ans)) 47 { 48 printf("%d = %d + %d\n",n,ans,prime[i]); 49 flag=1; 50 break; 51 } 52 } 53 } 54 if(flag==0) 55 printf("Goldbach‘s conjecture is wrong.\n"); 56 } 57 return 0; 58 }
POJ 2262 Goldbach's Conjecture (素数判断),布布扣,bubuko.com
POJ 2262 Goldbach's Conjecture (素数判断)
标签:des style blog http color os io strong
原文地址:http://www.cnblogs.com/clliff/p/3923614.html