</pre><pre code_snippet_id="483066" snippet_file_name="blog_20141012_3_4972174" name="code" class="cpp">#include <stdio.h> #include<math.h> #define N 101 int main(){ int i, j, line, a[N]; for (i = 2; i < N; i++) a[i] = i; for (i = 2; i <sqrt(double(N)); i++)//sqrt内部要用double型因此要强制转换 { for (j = i + 1; j < N; j++) { if (a[i]!= 0 && a[j]!= 0)//排除跟自身相等的素数 if (a[j] % a[i] == 0) a[j] = 0;//假设能除尽则把a[i]换为0 } } for (i = 2 ,line = 0; i < N; i++) { if (a[i] != 0) { printf("%5d",a[i]); line++; } if (line == 10) { printf(" ");//每十个数换一次行 line = 0; } } printf(" "); return 0; }
版权声明:本文博客原创文章,博客,未经同意,不得转载。