HDU 5104 Primes Problem

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5104


题面:

Primes Problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1776    Accepted Submission(s): 813


Problem Description
Given a number n, please count how many tuple(p1, p2, p3) satisfied that p1<=p2<=p3, p1,p2,p3 are primes and p1 + p2 + p3 = n.
 

Input
Multiple test cases(less than 100), for each test case, the only line indicates the positive integer  n(n10000) .
 

Output
For each test case, print the number of ways.
 

Sample Input
   
   
   
   
3 9
 

Sample Output
   
   
   
   
0 2
 

Source
BestCoder Round #18
 

解题:

唉,说好不刷水题的了,又刷,真是不好...


代码:

#include <iostream>
#include <cmath>
using namespace std;
int store_prime[10000],cnt=0;
bool is_prime(int x)
{
	if(x==2)return true;
	int up_limit=sqrt(1.0*x);
	for(int i=2;i<=up_limit;i++)
	{
		if(x%i==0)return false;
	}
	return true;
}
int main()
{
	for(int i=2;i<=10000;i++)
	{
		if(is_prime(i))
		{
			store_prime[cnt++]=i;
		}
	}
	int tmp,sum,n,ans;
	while(cin>>n)
	{
		ans=0;
		for(int i=0;i<cnt;i++)
		{
			for(int j=i;j<cnt;j++)
			{
				tmp=n-store_prime[i]-store_prime[j];
				if(tmp>=store_prime[j]&&is_prime(tmp))
				ans++;
			}
		}
		cout<<ans<<endl;
	}
	return 0;
} 


你可能感兴趣的:(入门,HDU,BestCoder,最水题)