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;
}