【codeforces26A】Almost Prime
2015-09-04 20:19
417 查看
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:
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; }
相关文章推荐
- java下io文件切割合并功能加配置文件
- poj 3685 Matrix(二分搜索之查找第k大的值)
- 9-4第二次简单实验
- linux怎样从vi退出到shell
- uva1368 - DNA Consensus String
- 微软Win 10操作系统CPU使用率居高不下怎么办?试试这些方法
- 神经网络的cost function
- 剑指offer 二进制中1的个数
- [华为MU203] 使用AT命令实现网络访问
- ubuntu下chromium浏览器flash插件安装
- linux+iptables搭建网关服务器
- 剑指offer——面试题:42(二)s的连续正数序列
- usaco Agri-Net
- shell syntax
- ubuntu下修改文件夹权限
- Spring减少MVC的代码冗余
- Windows多线程基础
- Java入门-泛型
- HttpServlect详情
- CCF 201403-3 命令行选项 (恶心的模拟)