POJ 1595 素数筛法

简介:

题意让求一段素数中间的几个素数 用素数筛法筛出范围内的素数然后确定一下就行了 注意题目中1也算作素数了 具体看代码

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define max 2000
bool isprime[max];
int prime[max],nprime;
void getprime()
{
    memset(isprime,1,sizeof(isprime));
    isprime[1]=0;
    long long i,j;
    nprime=1;
    prime[1]=1;
    for(i=2; i<max; i++)
    {
        if(isprime[i])
            prime[++nprime]=i;
        for(j=i*i; j<max; j+=i)
            isprime[j]=0;
    }
}
int main()
{
    getprime();
    int n,c,f=0;
    while(cin>>n>>c)
    {
        int s;
        if(f)
            cout<<endl;
        f=1;
        for(s=1; prime[s]<=n; s++);
        s--;
        cout<<n<<" "<<c<<": ";
        if(2*c>=s)
            for(int i=1; i<=s; i++)
                if(i!=s)
                    cout<<prime[i]<<" ";
                else
                    cout<<prime[i]<<endl;
        else if(s%2)
            for(int i=s/2+1,j=-c+1; j<=c-1; j++)
                if(j!=c-1)
                    cout<<prime[i+j]<<" ";
                else
                    cout<<prime[i+j]<<endl;
        else if(s%2==0)
            for(int i=s/2,j=-c+1; j<=c; j++)
                if(j!=c)
                    cout<<prime[i+j]<<" ";
                else
                    cout<<prime[i+j]<<endl;
    }
    return 0;
}


目录
相关文章
|
7月前
|
人工智能
POJ 3104 Drying
POJ 3104 Drying
|
存储
|
人工智能
POJ 2531
初学dfs参考别人代码,如有雷同,见怪不怪。#include using namespace std; int aa[25][25]; int maxa=0; int step[25]={0},n; void dfs(int a,int b) { int t=b; step...
676 0
POJ 1804
题目:http://poj.org/problem?id=1804 大意:给你一串数字,排序。求出最少的交换次数  \ 我用归并做的 #include #include using namespace std; int aa[500010],bb[500010]; long lon...
671 0
poj 1455
Description n participants of > sit around the table. Each minute one pair of neighbors can change their places.
599 0
|
人工智能 BI
poj-3185-开关问题
描述   牛一行20他们喝的水碗。碗可以那么(面向正确的为清凉水)或颠倒的(一个位置而没有水)。他们希望所有20个水碗那么,因此用宽鼻子翻碗。   嘴太宽,他们不仅翻转一碗还碗的碗两侧(总共三个或三个——在两端的情况下碗——两碗)。
784 0
|
并行计算 网络架构
poj-1005-l tanink i need a houseboat
Description Fred Mapper is considering purchasing some land in Louisiana to build his house on. In the process of investigating the land, he learned ...
938 0
|
机器学习/深度学习