CF 26A-Almost prime

5 篇文章 0 订阅
4 篇文章 0 订阅

                                                                                                 Almost Prime

                                                                    Time Limit:2000MS    Memory Limit:262144KB    64bit IO Format:%I64d & %I64u

          Description

                 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 Input

     Input
   10
     Output
   2
    Input
   21
    Output
   8
 

思路:

由给出的范围可得素数范围:0~1500 (3000/2=1500);

筛法得到0~x/2(x为所给值)之间的素数;

理解题意:题目要求的一类殆素数的特征是:仅由两个素数通过乘积或乘方变化的来 因此按序模素数只有两个能被整除则说明该数符合题意,ans++;

#include<cstdio>
#include<iostream>
using namespace std;
int prime[1501];
int main()
{
	int x;
	while(scanf("%d",&x)!=EOF)
	{
		int i,j;
		prime[0]=0;
		prime[1]=0;
		for(i=2;i<x/2+1;i++) prime[i]=1;
		for(i=1;i<x/2;i++)
		{
			if(prime[i]) for(j=2*i;j<x/2+1;j+=i) prime[j]=0;			  	
		}
		int num,count,ans=0;
		for(num=6;num<=x;num++)
		{
			count=0;
			for(j=0;j<num/2+1;j++) 
   				{
				   if(prime[j]==0)continue;
			       if(num%j==0) count++;
   				   if(count==3) break;}
		    if(count==2) ans++;
		}
		printf("%d\n",ans);
	}
	return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值