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
 

解题:

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


代码:

#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;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值