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. .NET Core + gRPC 实现数据串流 (Streaming)

    引入 gRPC 是谷歌推出的一个高性能优秀的 RPC 框架,基于 HTTP/2 实现.并且该框架对 .NET Core 有着优秀的支持.最近在做一个项目正好用到了 gRPC,遇到了需要串流传输的问题. ...

  2. django media配置

    当我们需要向服务器发送图片或视频,需要对这些媒体文件进行保存时,需要指定保存在哪并将保存的路径添加到路由中. 1.设置settings.py MEDIA_URL = '/media/' MEDIA_R ...

  3. ML.NET---.NET下的机器学习引擎(简介)

    ML.NET 是一个跨平台的开源机器学习框架,它可以使 .NET 开发人员更容易的开展机器学习工作. ML.NET 允许 .NET 开发人员开发自己的模型,即使没有机器学习的开发经验,也可以很容易的将 ...

  4. Webserver asp配置及伪静态设置

    Webserver  IIS asp配置及伪静态设置 一.概述: 在Windows Server 2003系统中,用户可以借助IIS 6.0配置基于ASP.PHP.asp.NET等语言的动态Web网站 ...

  5. leetcode 18 4Sum JAVA

    题目 给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出 ...

  6. python: list转字符串

    命令: ' '.join(list) 其中,引号中是字符之间的分割符,如“,”,“;”,“\t”等等 如: list = [1, 2, 3, 4, 5] ''.join(list) 结果即为:1234 ...

  7. (1)python的基础认知

    Python程序员的信仰:人生苦短,我用python! (一)python的发展史 1989年开发的语言,创始人范罗苏姆(Guido van Rossum),别称:龟叔(Guido).为了打发圣诞节假 ...

  8. react.js学习之路六

    学习react中,我一直认为,总组件里面才有构造函数,但是我才发现我的观点是错误的,构造函数是可以出现在子组件里面的. 今天有一个错误是点击增加/减少input框里面 的数值 我一直在寻找input框 ...

  9. leecode刷题(11)-- 反转字符串

    leecode刷题(11)-- 反转字符串 反转字符串 描述: 编写一个函数,其作用是将输入的字符串反转过来. 示例 1: 输入: "hello" 输出: "olleh& ...

  10. Tomcat入门级小白教程

    Tomcat 类似与一个apache的扩展型,属于apache软件基金会的核心项目,属于开源的轻量级Web应用服务器,是开发和调试JSP程序的首选,主要针对Jave语言开发的网页代码进行解析,Tomc ...