超级素数(sprime) (BFS)

问题 G: 超级素数(sprime)

时间限制: 1 Sec  内存限制: 64 MB
提交: 47  解决: 11
[提交][状态][讨论版]

题目描述

超级素数是指一个素数,每去掉后面一个数字,总能保证剩下的数为质数,例如:
373->37->3
这是一个长为3的超级素数。

输入

输入一个整数n (10≤n≤108)。

输出

从小到大输出所有小于等于n的超级素数,每个超级素数之间留一个空格。末尾也有一个空格

样例输入

10

样例输出

2 3 5 7 
【分析】通过找规律,发现所求素数中只可能出现1,2,3,5,7,9,而且1,9不能出现在首位,2,5只能出现在首位,然后就是BFS暴搜了。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define inf 0x3f3f3f3f
#define mod 1000000007
typedef long long ll;
using namespace std;
const int N=100010;
int n,dp[N],len;
int w[21][21];
int g[3];
int b,c;
int a[10]={1,3,5,7,9};
set<int>p;
 
 
bool charge(int x)
{
    for(int j=2;j<=sqrt(x);j++)
    {
        if(x%j==0)return false;
    }
    return true;
}
void bfs()
{
    queue<int>q;
    q.push(2);q.push(3);q.push(5);q.push(7);
    while(!q.empty())
    {
        int t=q.front(); p.insert(t);
        q.pop();
        for(int i=0;i<5;i++)
        {
            int k=t*10+a[i];
            if(charge(k)&&k<=n)
            {
 
                q.push(k);
            }
        }
    }
}
 
int main() {
    memset(w,0,sizeof(w));
    cin>>n;
    bfs();
    for(set<int>::iterator it=p.begin();it!=p.end();it++)
    {
        cout<<*it<<" ";
    }cout<<endl;
    return 0;
}

 


转载于:https://www.cnblogs.com/jianrenfang/p/5719852.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值