A. Almost Prime
time limit per test
2 secondsmemory limit per test
256 megabytesinput
standard inputoutput
standard outputA 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 /* 2 author:OVRee 3 Time:2015-3-6 14:38:28 4 题目大意: 5 输入n,输出1~n内仅包含两个质因子的数的个数,所谓质因子,就是质数,比如,30 有 3 个质因子:2;3;5 6 思路很简单:先建立素数表,遍历取模, 若质因子为 2 ,计数器就 ++; 7 */ 8 #include <iostream> 9 #include<bitset> 10 using namespace std; 11 #define INF 3005 12 /*set函数是典型的 “筛法 ”求素数 ,复杂度是 O(In(n)) -->复杂度怎么算不是很清楚 13 确定i是否为素数,结果储存在IsPrime[i]中, 14 用下标来来表示每个数,若 i 是素数,则标记Isprime[i] 为 1 15 如果不使用bool类型,则结果会得到0;推测是内存爆了的缘故,很奇怪吧--*/ 16 //其实IsPrime[]我想用 bitset 型,但没有成功 17 18 void Set( bool IsPrime[]) 19 { 20 int i,j; 21 for(i = 0; i <= INF; ++i) 22 IsPrime[i] = 1; //先全部标记为 1 23 IsPrime[0] = IsPrime[1] = 0; // 0 和 1 不是素数,所以标记为 0; 24 for(i=2 ; i <= INF; ++i) 25 { 26 if(IsPrime[i]) 27 { 28 for(j = 2*i; j <= INF; j += i) 29 IsPrime[j] = 0 ; 30 } 31 } 32 } 33 34 int main() 35 { 36 bool isPrime[INF]; 37 int n; 38 int cnt = 0; 39 cin >> n; 40 int i,j = 0; 41 int a[1000] = {0}; 42 Set(isPrime); 43 for(i = 0; i < INF; i++) 44 { 45 if(isPrime[i]) 46 { 47 a[j] = i; 48 //cout << a[j] <<endl; 49 j++; 50 } 51 } 52 for(i = 1; i <= n; i++) 53 { 54 int two = 0; 55 for(j = 0; a[j] < i;j++) 56 { 57 if(i % a[j] == 0) 58 two++; 59 } 60 if(two == 2) 61 cnt ++; 62 } 63 cout << cnt; 64 return 0; 65 }