P3912 素数个数

题目地址


#include<cstdio>
#include<iostream>
#include<cmath>
using namespace std;
const int MAXN=1e8;
bool notPrime[MAXN];
int notPrimeCnt=0;
void initPrime(int x){
	int tot=sqrt(x+0.5);
	notPrimeCnt++;
	for(int i=2;i<=tot;i++){
		if(notPrime[i])continue;
		for(int j=i*i;j<=x;j+=i){
			if(!notPrime[j]){
				notPrime[j]=1;
				notPrimeCnt++;
			}
		}
	}
}
int main(){
	int n;
	scanf("%d",&n);
	initPrime(n);
	printf("%d
",n-notPrimeCnt);
	return 0;
}
原文地址:https://www.cnblogs.com/zbsy-wwx/p/11680521.html