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 3008216013 at 2010-04-12 22:43:15 on Problem 2739
#include<iostream>
#include<cmath>
using namespace std;
bool prime(int n)
{
        int i;
	if(1<n&&n<4) return 1;
	else
	{
		for(i=2;i<=(int)sqrt(n*1.0);i++)
		{
				if(n%i==0)break;
		}
		if(n%i==0)return 0;
		else return 1;
	}
}

int main(void)
{
	int a,si,b[20000],g,count,h;
	cin>>a;
	while(a)
	{
		g=a;
		si=count=0;
		while(g>1)
		{
			if(prime(g--))b[si++]=g+1;
		}
		for(int i=0;i<si;i++)
		{
			if(a==b[i])count++;
			else 
			{
				h=a;
				h=h-b[i];
				for(int j=i+1;j<si;j++)
				{
					if(h==b[j]){count++;break;}
					else if(h<b[j])break;
					else h=h-b[j];
				}
			}
		}
		cout<<count<<endl;
		cin>>a;
	}
	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