HDU 5104 Primes Problem(打表)

来源:互联网 发布:medline数据库论文 编辑:程序博客网 时间:2024/06/05 19:03

传送门: http://acm.hdu.edu.cn/showproblem.php?pid=5104

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(n≤10000).

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

Sample Input
3
9

Sample Output
0
2

把我wa哭了 判断结果那 写成prime[j]&&prime[i] 就时间超限
prime[i]&&prime[j] 就过了 不是很懂

code:

#include<cstdio>#include<iostream>//#include<bits/stdc++.h>#include <string.h>using namespace std;int is_prime(int n ){    for(int i=2 ;i*i<=n;i++ )    {        if ( n % i ==0)            return 0;    }    return 1;}int main(){    int prime[10005];    memset(prime, 0 ,sizeof(prime));//    for (int i=2; i<=10002; i++)//    {//        int flag=0;//        for (int j=2; j*j<=i;j++)//        {//            if (i%j==0)//            {//                flag=1;//                break;//            }////        }//        if (flag==0)//            prime[i]=1;////    }for(int i=2 ;i<=10002;i++)    {        if ( is_prime(i) )            prime[i] = 1;    }    int n;    while (cin>>n)    {        int num=0;        for (int i=2; i<=n; i++)        {            for (int j=i; j<=(n-i)/2; j++)            {                if (prime[i]&&prime[j]&&prime[n-i-j])                {                    num++;                }            }        }        cout<<num<<endl;    }    return 0;}