原题网址:
http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0009
Prime Number
Write a program which reads an integer n and prints the number of prime numbers which are less than or equal to n. A prime number is a natural number which has exactly two distinct natural number divisors: 1 and itself. For example, the first four prime numbers are: 2, 3, 5, 7.
Input
Input consists of several datasets. Each dataset has an integer n (n ≤ 999999) in a line.
The number of datasets ≤ 30.
Output
For each dataset, prints the number of prime numbers.
Sample Input
10 3 11
Output for the Sample Input
4 2 5
题意:
给出n ,输出不大于n的素数有多少个
题解:
打表筛选的时候进行统计
/*
http://blog.csdn.net/liuke19950717
*/
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn= 999999+5;
ll prime[maxn];
int main()
{
ll cnt=0;
for(ll i=2;i<maxn;++i)
{
if(!prime[i])
{
prime[i]=++cnt;
for(ll j=i*i;j<maxn;j+=i)
{
prime[j]=1;
}
}
else
{
prime[i]=prime[i-1];
}
}
ll n;
while(~scanf("%lld",&n))
{
printf("%lld\n",prime[n]);
}
return 0;
}