qduoj LC and Prime&&hdu 5104(线性筛)

原文:https://qduoj.com/problem/9/点击打开链接


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.

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

For each test case, print the number of ways.

3
9
0
2


本来以为需要线性筛 但是貌似暴力选素数也能过 while语句里用scanf会超时  换成cin就不会 不知道为什么 不知可有大佬能解释下 

简单的打表+二重循环 另外注意在循环的时候要有从小到大选 不然会重复

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
int a[10000];
int main()
{
    for(int i=2;i<10000;i++)
    {
        int n=i;
        if(a[i]==0)
        while(n<10000)
        {
            n+=i;
            if(n<10000)
            {
                a[n]=1;
            }
        }
    }
    a[0]=1;
    a[1]=1;
    int m=0;
    int b[10000];
    int head=0;
    for(int i=0;i<10000;i++)
        if(a[i]==0)
        {
            b[head]=i;
            head++;
        }
    while(cin>>m)
    {
        int num=0;
        for(int i=0;i<head;i++)
            for(int j=i;j<head;j++)
                if((m-b[i]-b[j])>=b[j])
                {
                    if(a[m-b[i]-b[j]]==0)
                    num++;
                }
        printf("%d\n",num);
    }

}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值