【codeforces26A】Almost Prime


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

题意:输出小于等于n的有且只有两个质因数的个数。

解题思路:本来看数据3000,就想着纯打表,后来看看,也还有蛮多,就老老实写吧,先用prime数组存1-1500之间的素数,然后判断小于等于n之间的满足题意的个数。

code:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
using namespace std;
int prime[250];
int is_prime(int n)
{
    for(int i=2;i<=n/2;i++)
        if(n%i==0)
           return 0;
    return 1;
}
bool is_alpme(int i)
{
	bool flag=false;
	int pos=0,nbr=i;
	while(1)
	{
		if(prime[pos]>nbr)
            return false;
		if(flag==false && nbr%prime[pos]==0)
		{
			flag=true;
			while(nbr%prime[pos]==0)
                nbr=nbr/prime[pos];
		}
		else if(flag==true && nbr%prime[pos]==0)
		{
			nbr=nbr/prime[pos];
			while(nbr%prime[pos]==0)
                nbr=nbr/prime[pos];
			if(nbr==1)
                return true;
			else
                return false;
		}
		if(prime[pos]==1499)
            return false;
		pos++;
	}
}
int main()
{
    int cnt=0;
    int res=0;
    int n;
    for(int i=2;i<1500;i++)
        if(is_prime(i)){
            prime[res++]=i;
        }
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
	{
		if(is_alpme(i))
            cnt++;
	}
    printf("%d\n",cnt);
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值