Prime Cuts
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 10610   Accepted: 4046

Description

A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In this problem you are to write a program that will cut some number of prime numbers from the list of prime numbers between (and including) 1 and N. Your program will read in a number N; determine the list of prime numbers between 1 and N; and print the C*2 prime numbers from the center of the list if there are an even number of prime numbers or (C*2)-1 prime numbers from the center of the list if there are an odd number of prime numbers in the list.

Input

Each input set will be on a line by itself and will consist of 2 numbers. The first number (1 <= N <= 1000) is the maximum number in the complete list of prime numbers between 1 and N. The second number (1 <= C <= N) defines the C*2 prime numbers to be printed from the center of the list if the length of the list is even; or the (C*2)-1 numbers to be printed from the center of the list if the length of the list is odd.

Output

For each input set, you should print the number N beginning in column 1 followed by a space, then by the number C, then by a colon (:), and then by the center numbers from the list of prime numbers as defined above. If the size of the center list exceeds the limits of the list of prime numbers between 1 and N, the list of prime numbers between 1 and N (inclusive) should be printed. Each number from the center of the list should be preceded by exactly one blank. Each line of output should be followed by a blank line. Hence, your output should follow the exact format shown in the sample output.

Sample Input

21 2
18 2
18 18
100 7

Sample Output

21 2: 5 7 11

18 2: 3 5 7 11

18 18: 1 2 3 5 7 11 13 17

100 7: 13 17 19 23 29 31 37 41 43 47 53 59 61 67

Source

 

题意:给定你一个数n,让你求出1-n内有多少个素数,再给你一个数d,如果2*d大于素数的个数则全部输出;

否则,如果个数为单数,输出2*d - 1个并且以中间那个素数为中心分别向两边输出d-1个;

如果偶数个,输出2*d个,也是以中间那个素数为中心向两边扩展;————网上查的

 #include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define max 1000
int prime[max+];
void get_prime(int n){
int i;
prime[]=;
prime[]=;
int index=;
for(i=;prime[index]<=n;i++){//得到第一个大于n的素数
int j=;//j不能==0!!
for(;j<=index&&prime[j]*prime[j]<=i;j++){
if(!(i%prime[j])){
break;
}
}
if(prime[j]*prime[j]>i){
prime[++index]=i;
}
}
//cout<<prime[index]<<endl;
}
int main()
{
get_prime(max);
int n,c;
while(cin>>n>>c){
int i=,j,s,e;
while(prime[i]<=n){
i++;
}
if(i%){
if(i<*c-){
s=;
e=i-;
}
else{
s=i/-(c-);
e=i/+(c-);
}
}
else{
if(i<*c){
s=;
e=i-;
}
else{
s=i/--(c-);
e=i/+(c-);
}
}
cout<<n<<" "<<c<<":";
for(j=s;j<=e;j++){
cout<<" "<<prime[j];
}
cout<<endl;
cout<<endl;//格式注意
}
return ;
}

poj 1595 Prime Cuts的更多相关文章

  1. POJ 1595 Prime Cuts (ZOJ 1312) 素数打表

    ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=312 POJ:http://poj.org/problem?id=159 ...

  2. ACM/ICPC 之 数论-素数筛选法 与 "打表"思路(POJ 1595)

    何为"打表"呢,说得简单点就是: 有时候与其重复运行同样的算法得出答案,还不如直接用算法把这组数据所有可能的答案都枚举出来存到一个足够大的容器中去-例如数组(打表),然后再输入数据 ...

  3. poj 1595

    #include <iostream> #define N 10010 using namespace std; int a[N],b[N]; int prime(int a) { int ...

  4. [暑假集训--数论]poj1595 Prime Cuts

    A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In ...

  5. 双向广搜 POJ 3126 Prime Path

      POJ 3126  Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16204   Accepted ...

  6. poj 2689 Prime Distance(大区间素数)

    题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...

  7. POJ 3126 Prime Path(素数路径)

    POJ 3126 Prime Path(素数路径) Time Limit: 1000MS    Memory Limit: 65536K Description - 题目描述 The minister ...

  8. Miller_rabin算法+Pollard_rho算法 POJ 1811 Prime Test

    POJ 1811 Prime Test Time Limit: 6000MS   Memory Limit: 65536K Total Submissions: 32534   Accepted: 8 ...

  9. PoJ 1595 PrimeCuts

    Prime Cuts Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9339   Accepted: 3562 Descri ...

随机推荐

  1. 关于C#里面SQLite读取数据的操作

    做C#朋友的一个获取DataSet函数,对C#不熟,整理整理,了解怎么用 //挂载表格时候用 public static DataSet Query(string SQLString) { using ...

  2. Oracle数据表转换为Shapefile(一)

    严格来说,文章标题中的“转换”并不完全合适.本文的主要内容是基于Oracle数据表的数据来生产出Shapefile文件.进行该工作的一个前提条件是:Oracle数据表中包含坐标数值字段,一般来说就是x ...

  3. [LeetCode 题解]: Permutation Sequcence

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  4. WEB应用从服务器主动推送的方法

    1.短轮询 2.长轮询 3.iframe 4.sse 5.Web Scoket

  5. sql添加列,删除列,修改列

    有时候,当数据库里面有数据了,再到设计器里面删除列,就删不掉.只能用代码删除. alter table tableName drop column columnName 添加列 ) 修改列 ) --修 ...

  6. 【windows】dos命令查看某个文件夹下所有文件目录列表

    dos命令  dir展示一个目录中的文件夹和文件列表  /a代表显示隐藏目录

  7. JDK安装目录分析-两个jre和三个lib

    安装JDK后,Java目录下有jdk和jre两个目录,但jdk下还有一个jre目录,而且这个jre比前面那个jre在bin目录下少了个server文件夹(Server端的Java虚拟机)!前一个jre ...

  8. 二十二、Node.js-get&post

    get: 前台代码: <body> <h1>登录</h1> <form action="/dologin" method="ge ...

  9. js中闭包和对象相关知识点

    学习js时候,读到几篇不错的博客.http://www.cnblogs.com/yexiaochai/p/3802681.html一,作用域 和C.C++.Java 等常见语言不同,JavaScrip ...

  10. docker kafka 修改hostname导致的问题

    昨天发现开发环境的3台kafka无法消费,所以今日kafka的容器执行如下语句 bash-4.4# ./kafka-topics.sh --describe --zookeeper 192.168.0 ...