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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值