JD 题目1040:Prime Number (筛法求素数)
OJ题目:click here~~
题目分析:输出第k个素数

依次类推。直到筛子为空时结束。
如有:
AC_CODE
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream> using namespace std ;
const int N = 1000000;
bool prime[N] ;
void initprime(){
memset(prime, 1 , sizeof(prime)) ;
prime[0] = prime[1] = 0 ;
double t = sqrt(N) + 1 ;
for(int i = 2;i < t;i++){
if(prime[i]){
for(int j = i + i;j < N;j += i)
prime[j] = false ;
}
}
} int main(){
initprime();
int k , t ;
while(cin >> k){
t = 0 ;
for(int i = 0;i < N;i++){
if(prime[i]){
t++;
if(t == k){
cout << i << endl ;
break ;
}
}
}
}
return 0 ;
}
JD 题目1040:Prime Number (筛法求素数)的更多相关文章
- POJ2739_Sum of Consecutive Prime Numbers【筛法求素数】【枚举】
Sum of Consecutive Prime Numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 19350 Ac ...
- 2018牛客网暑期ACM多校训练营(第三场) H - Diff-prime Pairs - [欧拉筛法求素数]
题目链接:https://www.nowcoder.com/acm/contest/141/H 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
- hdu 4548 筛法求素数 打表
题目:http://acm.hdu.edu.cn/showproblem.php?pid=4548 Problem Description 小明对数的研究比较热爱,一谈到数,脑子里就涌现出好多数的问题 ...
- UVA 10006 - Carmichael Numbers 数论(快速幂取模 + 筛法求素数)
Carmichael Numbers An important topic nowadays in computer science is cryptography. Some people e ...
- <转载>一般筛法和快速线性筛法求素数
素数总是一个比较常涉及到的内容,掌握求素数的方法是一项基本功. 基本原则就是题目如果只需要判断少量数字是否为素数,直接枚举因子2 ..N^(0.5) ,看看能否整除N. 如果需要判断的次数较多,则先用 ...
- 蓝桥杯 算法训练 Torry的困惑(基本型)(水题,筛法求素数)
算法训练 Torry的困惑(基本型) 时间限制:1.0s 内存限制:512.0MB 问题描述 Torry从小喜爱数学.一天,老师告诉他,像2.3.5.7……这样的数叫做质数.Torry突 ...
- Algorithm --> 筛法求素数
一般的线性筛法 genPrime和genPrime2是筛法求素数的两种实现,一个思路,表示方法不同而已. #include<iostream> #include<math.h> ...
- 筛法求素数Java
输出:一个集合S,表示1~n以内所有的素数 import java.util.Scanner; public class 筛法求素数 { public static void main(String[ ...
- 埃氏筛法求素数&构造素数表求素数
埃氏筛法求素数和构造素数表求素数是一个道理. 首先,列出从2开始的所有自然数,构造一个序列: 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 1 ...
随机推荐
- MongoDB学习笔记(11) --- 聚合
MongoDB中聚合(aggregate)主要用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果.有点类似sql语句中的 count(*) aggregate() 方法 MongoDB中聚 ...
- Oracle 12C -- clone a remote pdb
Connect to the remote CDB and prepare the remote PDB for cloning. SQL> select con_id,dbid,name,op ...
- [Perforce]password (P4PASSWD) invalid or unset. 的错误解决
前言 使用 Perforce , 能够使用Perforce 的Client 端. 有时候在编写一些脚本或代码的时候, 可能或使用到 Perforce的命令的方式. 正常状况下. 使用例如以下命令: p ...
- shell curl 下载图片并另存为(重命名)
curl -o fuck.png http://img30.360buyimg.com/imgzone/jfs/t19711/232/1837927836/150222/e4cd87bb/5ad990 ...
- 转 Kubernetes 入门 概念理解
你闺女也能看懂的插画版Kubernetes指南 原创 2016-06-30 作者 周小璐 译 编者按:Matt Butcher是Deis的平台架构师,热爱哲学,咖啡和精雕细琢的代码.有一天女儿走进书 ...
- 创建多模块springcloud应用eureka server和client和消费端demo
使用环境是 STS + maven 1 创建父级 项目,springcloud-demo1 new -> maven project -> 按照要求进行配置即可.然后删除 src目录,因为 ...
- mysql打开binlog
修改mysql的配置文件,ubuntu下mysql的配置文件存放位置为:/etc/mysql/my.cnf 找到log_bin配置项,指定一个路径: 重启数据库:/etc/init.d/mysql r ...
- 定期删除elasticsearch 的index 索引
#!/bin/bashfind /data/elasticsearch/data/pro-kz-log/nodes/0/indices/ -type d -mtime +7 | awk -F" ...
- php的opcode缓存
前言:由php的运行机制决定,其实php在运行阶段我们也是可以进行缓存的从而提高程序运行效率,这就是我们常说的opcode缓存.1.简述php的运行机制(因为本文是写opcode缓存的所以这里只是简要 ...
- HTML5学习笔记(三):语义化和新增结构元素
在HTML5之前,使用机器来阅读一个网页是非常困难的,我们使用不同样式的div来标记不同的内容,所以实际上机器无法得知页面的哪个部分是正文,哪个部分是标题,那么在HTML5里,针对这个问题就引入了语义 ...