hdu 4715(Difference Between Primes)

题目链接:点击打开链接

题目大意:give you an even , you should output  the difference of 2 primes

题目分析:even though it says if you cannot find the 2 primes you may printf(" fail")

                    yet,if you think carefully you will find that there must exist    such primes.--->

                   

题目总结:明明清晰明了的代码,runtime error了= =

                   又是sb 的小错误,什么时候才能改掉啊……

                    if(flag=-1)  x=0-x;   有点怪异吧~!!! 应该是if (flag ==-1) x=0-x;

                   说多了是泪==

#pragma comment(linker, "/STACK:16777216")//为了不re还开了栈开关- -
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
using namespace std;

bool is_prime[1000800];//用之前的算法得出1000004之内的数都可以满足猜想,并且最大的ans才是1000151,所以筛选质数自然就可以确定范围;了

void init()
{
    for(int i=0; i<1000800; i++)
        is_prime[i]=true;
    for(int i=2; i*i < 1000800;i++) if(is_prime[i])
    {
        for(int j=2*i; j<1000800; j+=i)
            is_prime[j]=false;
    }

}
void solve(int x,int flag)
{
    if(flag==-1) x=0-x;
    int i=x+2;

    while(i<1000800)
    {

        if(is_prime[i]&&is_prime[i-x])
        {
            if(flag==1)
                printf("%d %d\n",i,i-x);
            else
                printf("%d %d\n",i-x,i);
            break;
        }
        i++;
    }

}
int main()
{
    int n,x;
    init();
    scanf("%d",&n);
    while(n--)
    {
        scanf("%d",&x);
        if (x<0)
            solve(x,-1);
        else
            solve(x,1);

    }

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值