Almost Prime
题面翻译
题目描述
一个数如果恰好有两个不同质因子,那它被叫做几乎是质数的数。例如:6,18,24
就是几乎是质数的数,而4,8,9,42
不是,请你找出
1
1
1 到
n
n
n 中几乎是质数的数有多少个。
输入格式
输入仅包含一个整数 n n n( 1 ≤ n ≤ 3000 1\leq n\leq3000 1≤n≤3000)。
输出格式
输出 1 1 1 到 n n n 中几乎是质数的数的数量。
Translated by Khassar
题目描述
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 contains one integer number $ n $ ( $ 1<=n<=3000 $ ).
输出格式
Output the amount of almost prime numbers between 1 and $ n $ , inclusive.
样例 #1
样例输入 #1
10
样例输出 #1
2
样例 #2
样例输入 #2
21
样例输出 #2
8
思路
我们可以用类似筛选法的思路来解决,只要同一个数被加了两次就满足题意。
代码
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 3e5+10;
int b[N];
int n;
int ans;
int main(){
cin>>n;
b[1]=1;
for(int i=2;i<=n;i++){
if(!b[i]){
for(int j=2;i*j<=n;j++){
b[i*j]++;
}
}
}
for(int i=1;i<=n;i++){
if(b[i]==2){
ans++;
}
}
cout<<ans;
return 0;
}