C - Almost Prime

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.

Example
Input

10

Output

2

Input

21

Output

8
解题说明:题目中要求是找出只含有两个因子的数字,求出该数有哪些因子,可以用数组来存放每个数对应的因子总数,循环判断即可。
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF){
        int a[5000]={0},num=0;
        for(int i=2;i<=n;i++){
            for(int j=2;j<i;j++){
                if(a[j]==0&&i%j==0)
                    a[i]++;
            }
            if(a[i]==2)
                num++;
        }
        printf("%d\n",num);
    }
    return 0;
}
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<math.h>
using namespace std;
int disprime(int n){
    int i,t,flag=0,counter=0;
    for(i=2,t=n;i<n;i++){
        while(t>0){
            if(t%i==0)
                t/=i;
            else
                break;
            flag=1;
        }
       if(flag)
            counter++;
       flag=0;
    }
    return counter;
}

int main()
{
    int n,num=0;
    while(scanf("%d",&n)!=EOF){
    for(int i=1;i<=n;i++){
        if(disprime(i)==2)
            num++;
    }
    printf("%d\n",num);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值