题解 | #筛选法求素数#
筛选法求素数
http://www.nowcoder.com/practice/06c3dcc303654ef4926654023eca1e5a
#include<stdio.h>
int main()
{
int n = 0;
scanf("%d",&n);
int arr[100] = { 0 };
int i = 2, b = 0;
for (; i <= n; i++)
{
arr[b++] = i;
}
int c=0;
for (i = 0; i < b; i++)
{
for (int f = 2; f <= n; f++)
{
if (arr[i] > f && arr[i] % f == 0)
{
arr[i] = 0;
c++;
}
}
if (arr[i] != 0)
printf("%d ", arr[i]);
}
printf("\n%d", c);
return 0;
}