来源:https://blog.csdn.net/u011815404/article/details/79451627
作者:Alex_McAvoy
#include<iostream>
#include<cmath>
using namespace std;
bool judge(int x);
int main()
{
int n;
int i;
int sum=0;
cin>>n;
for(i=2; i<=n; i++)
if(judge(i))//若是素数
sum++;//累加素数个数
cout<<sum<<endl;
return 0;
}
bool judge(int x)//判断素数
{
int i=2;
while(i<=floor(sqrt(x))&&(x%i)!=0)
i++;
if(i>floor(sqrt(x)))
return true;
return false;
}
要点:1.bool 返回的是true 或false
2.floor的解释:
例如double floor ( double x );返回不大于x的最大整数
floor of 2.3 is 2.0
floor of 2.6 is 2.0
floor of -2.3 is -3.0
floor of -2.6 is -3.0