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

做了好多关于素数的题,这个算是比较综合的,呵呵~~~~

Posted by xiaoyuanwang at 2007-05-04 16:27:23 on Problem 2739
#include<iostream>
using namespace std;
void creat_prime(int *prime,int n)
{
	int flag=2;
	int t=3;
	int i,j;
	for(i=7;i<n;i+=flag)
	{
		flag=6-flag;
		for(j=0;prime[j]*prime[j]<i;j++)
		{
			if(i%prime[j]==0)break;
		}
		if(prime[j]*prime[j]>i)
		{
			prime[t]=i;
			t++;
		}

	}

}
int main()
{
	int i,j,k,n,sum,flag;
	int prime[2048]={2,3,5,0};
	creat_prime(prime,10000);
	while(cin>>n,n)
	{
		flag=0;
		for(i=1;i<=n/2;i++)
			for(j=0; ;j++)
			{
				sum=0;
				for(k=j;k<i+j;k++)
				{
					sum+=prime[k];
					if(sum>n)break;
				}
				if(sum==n)
					flag++;
				if(sum>n)break;
			}
			cout<<flag<<endl;
	}



	return 1;
}
我打了表,再枚举就OK了,希望高手指点

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