• 1.1好素数


    题目

    题意:一个好素数的定义是,他是一个素数,然后他的左右两边10区间内存在素数,那么他就是好素数,现在让你输入一个数字,这个数字以内的好素数的数量。

    解题方法:先把每一个数字是不是素数判断出来,打个表,要用筛法,或者用线性筛,然后在进行3到n的循环,如果他是素数,就判断他的左右两边是不是有没有素数,如果有,答案就加一。最后输出就可以了。

    代码

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include <iomanip>
    #include<cmath>
    #include<float.h> 
    #include<string.h>
    #include<algorithm>
    #include<vector>
    #include<queue>
    #define sf scanf
    #define pf printf
    #define scf(x) scanf("%d",&x)
    #define scff(x,y) scanf("%d%d",&x,&y)
    #define prf(x) printf("%d
    ",x) 
    #define mm(x,b) memset((x),(b),sizeof(x))
    #define rep(i,a,n) for (int i=a;i<n;i++)
    #define per(i,a,n) for (int i=a;i>=n;i--)
    typedef long long ll;
    const ll mod=1e9+7;
    const double eps=1e-8;
    const int inf=0x3f3f3f3f;
    using namespace std;
    const double pi=acos(-1.0);
    const int N=1e5+12;
    bool sushu[N]; 
    int judge(int a)    //判断是不是素数
    {
    	int l=a-10,r=a+10,temp=0;
    	if(l<1) l=1;//左边不能小于1
    	rep(i,l,a)
    	{
    		if(sushu[i]){temp++;break;	}  //有就加temp加一,然后跳出
    	}
    	rep(i,a+1,r+1)
    	{
    		if(sushu[i]){temp++;break;}//有就加temp加一,然后跳出
    	}
    	if(temp==2) return true;//要左右两边都有,那么之前就要加两次,必须是2 的时候才能符合好素数
    	return false;
    }
    int main()
    {
    	mm(sushu,true);//全都复制为
    	sushu[0]=sushu[1]=false;
    	int n;
    	scf(n);
    	rep(i,2,sqrt(n)+10)
    	{
    		if(sushu[i])        //如果是素数就进入筛选
    		{
    			for(int j=i*i;j<n+15;j+=i)//判断是不是素数要比n大一些,不能刚好到n不然不够用
        	    		    sushu[j]=false;
    		}
    	}
    	int ans=0;
    	rep(i,3,n+1)
    	{
    		if(sushu[i])
    		    if(judge(i))
    		        ans++;
    	}
    	prf(ans);
    	return 0;
    }
    
    
  • 相关阅读:
    保持同步
    将Cent0S 7的网卡名称eno16777736改为eth0
    Linux系统各发行版镜像下载(2)
    Linux系统各发行版镜像下载
    ~/.ssh目录找不到解决方法
    克隆后虚拟机网络配置
    新建的linux虚拟机找不到eth0解决办法
    SecureCRT 7 注册码
    linux运维常用命令
    shell脚本实例(2)
  • 原文地址:https://www.cnblogs.com/wzl19981116/p/10041753.html
Copyright © 2020-2023  润新知