</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("\n");//每十个数换一次行 line = 0; } } printf("\n"); return 0; }
时间: 2024-10-15 10:17:38