Almost Prime

Description
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
 
 
 
 
 
 
题解:
  素因子数的种类有且只有两种,称为Almost Prime。
       例如:18=2*3*3,只有2和3,是Almost Prime。
       求1~n Almost Prime 数量。
代码:
 1 #include <iostream>
 2 #include <cstdio>
 3 
 4 using namespace std;
 5 //计算数字n不同素因子个数
 6 int distinctPrime (int n);
 7 
 8 int main()
 9 {
10     int n, i, counter = 0;
11     cin >> n;
12     for(i=1; i<=n; i++){
13         if(distinctPrime(i) == 2)
14             counter ++;
15     }
16     cout << counter <<endl;
17     return 0;
18 }
19 
20 int distinctPrime (int n) {
21     int i, t, flag, counter = 0;
22     for(i=2, t=n; i<n; i++) {
23         while(t>0) {
24             if(t%i==0)
25                 t = t/i;
26             else
27                 break;
28             flag = 1;
29         }
30         if(flag)
31             counter++;
32         flag = 0;
33     }
34     return counter;
35 }

Problem -26A - Codeforces

转载请注明出处:http://www.cnblogs.com/michaelwong/p/4133164.html

 
原文地址:https://www.cnblogs.com/michaelwong/p/4133164.html