A. Almost Prime
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
A number is called almost prime if it has exactly two distinct prime divisors. For example, numbers 6, 18, 24 are almost prime, while 4, 8, 9, 42 are not. Find the amount of almost prime numbers which are between 1 and n, inclusive.
Input
Input contains one integer number n (1 ≤ n ≤ 3000).
Output
Output the amount of almost prime numbers between 1 and n, inclusive.
Sample test(s)
input
10
output
2
input
21
output
8
简单题目,题目描述了一类数
就是用筛法求素数,
然后利用相似的方法把要求的数打表
最后再把1到n的个数打表即可
#include <iostream>
#include <stdio.h>
using namespace std;
const int M=3005;
int pr[M];
int apr[M];
int napr[M];
int main()
{
for(int i=2;i<M;i++)
{
if(pr[i])
continue;
for(int j=2*i;j<M;j+=i)
pr[j]=1;
}
for(int i=2;i<M;i++)
{
if(pr[i])
continue;
for(int ii=i;ii<M;ii*=i)
for(int j=i+1;j<M;j++)
{
if(pr[j])
continue;
for(int jj=j;jj<M;jj*=j)
{
if(jj*ii>M)
break;
apr[jj*ii]=1;
}
}
}
int n;
int tot=0;
for(int i=1;i<M;i++)
{
if(apr[i])
tot++;
napr[i]=tot;
}
//for(int i=1;i<M;i++)
//cout<<napr[i]<<" ";
while(scanf("%d",&n)!=EOF)
{
printf("%d\n",napr[n]);
}
return 0;
}