题解 | #筛选法求素数#

筛选法求素数

https://www.nowcoder.com/practice/06c3dcc303654ef4926654023eca1e5a

#include <stdio.h>
#include <stdbool.h>

#define N 101
int main() {
    int n = 0;

    while (1 == scanf("%d", &n))
    {
        int count = 0;
        bool isprime[N] = {0};
        for (int i = 1; i <= n; i++)
        {
            isprime[i] = true;
        }
       for (int i = 2; i <= n; i++)
       {
            if (isprime[i])
            {
                printf("%d ", i);
                count++;
            }
            for (int j = i * i; j <= n; j += i)
            {
                isprime[j] = false;
            }
       } 
       printf("\n%d\n", n - count - 1);
    }    

    return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享
牛客网
牛客企业服务