Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
贴个代码吧,375MS,提交了三次,速度丝毫没变,如果有更快的,欢迎分享#include <iostream> #include <cstdio> using namespace std; const int N=78498; int primes[N]; void makePrimes(int num) { int i, j, cnt; primes[0] = 2; primes[1] = 3; for(i = 5, cnt = 2; cnt < num; i += 2) { int flag = true; for(j = 1; primes[j]*primes[j] <= i; ++j) { if(i%primes[j] == 0) { flag = false; break; } } if(flag) primes[cnt++] = i; } } bool isPrime(int n) { if(n < 2) return false; for(int i = 0; primes[i]*primes[i] <= n; ++i) if(n%primes[i] == 0) return false; return true; } int main() { int n; makePrimes(N); while(scanf("%d",&n) && n) { bool find=false; for (int i=0;i<N;i++) { if (2*primes[i]>n) break; if (isPrime(n-primes[i])) { printf("%d = %d + %d\n",n,primes[i],n-primes[i]); find=true; break; } } if (!find) printf("%s","Goldbach's conjecture is wrong.\n"); } return 0; } Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator