学会了不难。通过这道题学习了两点:

1:筛选法求素数。

2:在写比较长的程序的时候,给每个功能部分加上注释,思路会更清晰。

题意:

1.题目中所说的素数并不是真正的素数,包括1;

2.需要读懂题意,对于输入的n和c,如果1到n之间有偶数个素数则打印2c个数,奇数个素数则打印2c-1个数;

3.打印的数是所有素数中位于中间位置的那些数。

4.虽然数据量n<100.但是应确定第100+个素数是那个数,稍微把数组开大一些。

#include<iostream>
#include<cstdio>
#include<cmath>
using namespace std;
const int maxn=1050;
bool isprime[maxn];
int prime[200];
//筛选法求素数
void Erato()
{
isprime[0]=false;
isprime[1]=true;
isprime[2]=true;
//初始化
for(int i=3;i<maxn;i++){
isprime[i++]=true;//奇数
isprime[i]=false;//偶数
}
//判断素数
int n=sqrt(maxn);
for(int i=3;i<=n;i+=2)
for(int j=i+i;j<maxn;j+=i)
isprime[j]=false;
//将素数方如prime中,包括1
prime[0]=1;
prime[1]=2;
int j=2;
for(int i=3;i<maxn;i+=2)
if(isprime[i])
prime[j++]=i;
}
int main ()
{
Erato();
int n,c,count;
int printcount,str,end;
while(~scanf("%d%d",&n,&c))
{
printf("%d %d:", n, c);
count=0;
int i=0;
//统计素数的个数
while(prime[i++]<=n)
count++;
//统计要打印素数的个数
if(count%2)
printcount=2*c-1;
else
printcount=2*c;
// 计算数据的起始与终止位置
if(printcount>=count)
str=0,end=count-1;
else{
str=(count-printcount)/2;
end=str+printcount-1;
}
for(int i=str;i<=end;i++)
printf(" %d",prime[i]);
printf("\n\n"); }
return 0;
}

HDOJ 1319 Prime Cuts<数论>的更多相关文章

  1. poj 1595 Prime Cuts

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

  2. POJ1595 Prime Cuts

    Prime Cuts Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 11961   Accepted: 4553 Descr ...

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

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

  4. 【HDU】2866:Special Prime【数论】

    Special Prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  5. hdoj 1016 Prime Ring Problem

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...

  6. codeforces 680C C. Bear and Prime 100(数论)

    题目链接: C. Bear and Prime 100 time limit per test 1 second memory limit per test 256 megabytes input s ...

  7. HDOJ 1016 Prime Ring Problem素数环【深搜】

    Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...

  8. UVA 10140 - Prime Distance(数论)

    10140 - Prime Distance 题目链接 题意:求[l,r]区间内近期和最远的素数对. 思路:素数打表,打到sqrt(Max)就可以,然后利用大的表去筛素数.因为[l, r]最多100W ...

  9. Codefoces 432C Prime Swaps(数论+贪心)

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/26094917 题目连接:Codefoces ...

随机推荐

  1. 批处理 取得当前路径 %CD%

    在DOS的批处理中,有时候需要知道当前的路径.在DOS中,有两个环境变量可以跟当前路径有关,一个是%cd%, 一个是%~dp0. 这两个变量的用法和代表的内容一般是不同的. 1. %cd% 可以用在批 ...

  2. innerhtml 和value值有什么区别

    value 值写在标签里面的,innerHTML写在<button type="button" onclick="myFunction()">Try ...

  3. JavaScript高级程序设计:第十一章

    一.选择符API SelectorsAPILevel 1的核心是两个方法:querySelector()和querySelectorAll(). 1. querySelector()方法 queryS ...

  4. 使用putty上传文件到linux系统

    使用window的cmd命令 上传文件到linux  使用putty下的 pscp.exe pscp -r -l root -pw 1234567890 e:/htk 192.168.0.204:/r ...

  5. Box of Bricks

    Box of Bricks Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  6. javascript: 字符串拼接有问题

    点击某个链接,出现js错误:Uncaught SyntaxError: Unexpected token ILLEGAL 原因: $str .= sprintf('&nbsp&nbsp ...

  7. TypeConverter的使用

    我们知道,C#中有int.Parse,int.TryParse这样神奇的功能,那它们又是如何做到的呢?我们试着自己也来自定义一个“转换器”. 首先,定义一个类: public class Human ...

  8. zookeeper集群实例

    zookeeper是什么 Zookeeper,一种分布式应用的协作服务,是Google的Chubby一个开源的实现,是Hadoop的分布式协调服务,它包含一个简单的原语集,应用于分布式应用的协作服务, ...

  9. make 命令执行时,报错“missing separator stop”

    在Makefile文件中,命令必须以[tab]键开始.

  10. meta标签使360浏览器默认极速模式

    在head标签中添加一行代码: <html> <head> <meta name=”renderer” content=”webkit|ie-comp|ie-stand” ...