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

Re:哈哈难得一次AC纪念一下

Posted by et1239063237 at 2012-07-18 01:12:42 on Problem 2739
In Reply To:哈哈难得一次AC纪念一下 Posted by:et1239063237 at 2012-07-18 01:12:13
#include<iostream>
#include<cmath>

using namespace std;

//判断一个数是否为素数
bool isprime(int num)
{
	//cout<<num<<endl;

	if(num==2||num==3||num==5)
	{
		//cout<<"num是素数"<<endl;
		return true;
	}
	if(num<2||!(num%2)||!(num%3)||!(num%5))
	{
		//cout<<"num不是素数"<<endl;
		return false;
	}

	int n=7;
	int max=(int)sqrt((float)num);

	while(n<=max)
	{
		if(num%n==0)
		{
			//cout<<"num不是素数"<<endl;
			return false;
		}
		n+=2;
	}

	//cout<<"num是素数"<<endl;
	return true;
}

//判断一个数下一个质数是多少
int nextprime(int n)
{
	while(1)
	{
		n++;
		if(isprime(n))
			return n;
	}
}

int main()
{
	int prime[1230];
	int min,max,addition;

	int counter;
	prime[0]=0;
	prime[1]=2;
	for(counter=2;counter<1230;counter++)
		prime[counter]=nextprime(prime[counter-1]);

	int num;
	cin>>num;
	while(num!=0)
	{
		counter=0;
		max=1;
		min=0;
		addition=2;

		while(prime[max]<=num)
		{
			//cout<<"max:"<<prime[max]<<" min: "<<prime[min]<<" addition: "<<addition<<endl;
			if(addition==num)
			{
				counter++;
				max++;
				addition+=prime[max];
			}			
			else if(addition<num)
			{
				max++;
				addition+=prime[max];
			}
			else if(addition>num)
			{
				addition-=prime[min];
			    min++;
			}

		}

		cout<<counter<<endl;
		cin>>num;
	}

	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