4038 - 筛选法求素数
求10000以内的所有素数,每行10个,每个数之间一个空格隔开。
Input
Output
Examples
Input
Output
Solution C++
#include<iostream> #include<cstring> using namespace std; bool a[10005]; int main(){ memset(a,true,sizeof(a)); for (int i=2; i<=100; i++) if (a[i]) for (int j=2*i; j<=10000; j+=i) a[j]=false; int t=0; for (int i=2; i<=10000; i++) if (a[i]){ cout<<i; t++; if (t%10==0) cout<<endl; else cout<<" "; } cout<<endl; return 0; }