首页 > 其他 > 详细

usaco Superprime Rib

时间:2015-08-29 06:07:46      阅读:116      评论:0      收藏:0      [点我收藏+]

题意是求长度为N,的且,前i(i为,1,2,3.....N-1)位数字构成的数字都是素数的数字

例如2333,其中2,23,233,2333,都是素数

/*
ID: modengd1
PROG: sprime
LANG: C++
*/
#include <iostream>
#include <stdio.h>
using namespace std;
bool IsPrime(int x)
{
    if(x==1)
        return false;
    if(x==2)
        return true;
    if(x%2==0)
        return false;
    for(int i=3;i*i<=x;i++)
    {
        if(x%i==0)
            return false;
    }
    return true;
}
void slove(int deep,int limit,int now)
{
    if(deep==limit)
    {
        if(IsPrime(now))
            cout<<now<<endl;
        return;
    }
    for(int i=1;i<=9;i++)
    {
        if(IsPrime(now*10+i))
            slove(deep+1,limit,now*10+i);
    }
}
int main()
{
    freopen("sprime.in","r",stdin);
    freopen("sprime.out","w",stdout);
    int N;
    cin>>N;
    slove(0,N,0);
    return 0;
}

  

usaco Superprime Rib

原文:http://www.cnblogs.com/modengdubai/p/4768237.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!