筛选法球1-100内的素数

代码示例:

 1 #include<iostream>
 2 using namespace std;
 3 void main()
 4 {
 5     int a[100],i,j;
 6     for(i=1;i<100;i++)
 7         a[i]=i+1;
 8     for(i=1;i<100;i++)
 9     {
10         if(a[i]==0) continue;
11         for(j=i+1;j<100;j++)
12             if(a[j]%a[i]==0)
13                 a[j]=0;
14     }
15     for(i=1,j=0;i<100;i++)
16         if(a[i]!=0)
17         {
18             j++;
19             cout<<a[i]<<'\t';
20             if(j%5==0) cout<<endl;
21         }
22 }

运行结果:

posted @ 2012-06-28 21:02  iamvirus  阅读(336)  评论(0编辑  收藏  举报