蓝桥杯—用筛法求N内的素数

原题地址

参考代码

#include <iostream>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int is = 0;
    for(int i = 2;i<=n;i++)
    {
        is = 0;
        if(i == 2)
        {
            cout<<2<<endl;
        }
        else
        {
            for(int j = 2;j < i;j++)
            {
                if(i%j==0)
                {
                    is = 1;
                }
            }
            if(is == 0)
            {
                cout<<i<<endl;
            }
        }
    }
    return 0;
}
posted @ 2018-03-05 22:45  兰翔  阅读(1021)  评论(0)    收藏  举报