Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贴个代码吧,375MS,提交了三次,速度丝毫没变,如果有更快的,欢迎分享

Posted by luo223 at 2013-09-12 22:27:04 on Problem 2262
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator