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

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. c语言_头文件

    传统 C++ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #include <assert.h> //设定插入点 #include <ctyp ...

  2. c语言-三字符组

    C 源程序源字符集在 7 位 ASCII 字符集中包含,但设置为 ISO 646-1983 固定的代码的超集. 三字符序列允许 C 程序编写使用 " 仅 ISO (国际标准组织的固定的代码. ...

  3. java字符串格式化

    转自:JAVA字符串格式化-String.format()的使用(转) 常规类型的格式化 String类的format()方法用于创建格式化的字符串以及连接多个字符串对象.熟悉C语言的同学应该记得C语 ...

  4. java 多线程 一个博客

    http://blog.csdn.net/a352193394/article/category/2563875 Java多线程之~~~线程安全容器的非阻塞容器 在并发编程中,会经常遇到使用容器.但是 ...

  5. 认识cookie与session的区别与应用

    通常我们所说的浏览器自动保存密码,下次不用登陆,网页换皮肤,用户引导,提示一次就不再出现的内容,大部分通过cookie或者session来实现的,在这次制作用户引导中,本人就用到了cookie的内容, ...

  6. 发现在看完objc基本语法之后,还是看Apple文档比较有用。

    现在已经停止找中文资料了,因为很多例子已经过时,运行不出来. 看完objc基本语法以后,Apple的资料也看得懂了. 还是应该跟着Apple的入门指南开始学,今后也应该以Apple的文档为主.

  7. java 利用SMB读取远程文件

    package  com.yss.test.FileReadWriter; import  java.io.BufferedInputStream; import  java.io.BufferedO ...

  8. 克隆虚拟机系统整个文件快照,然后另起建立该系统,产生的IP地址冲突解决办法

    进入克隆后的文件系统 cd /etc/sysconfig/network-scripts/ cp ifcfg-eth0  ifcfg-eth1 vim ifcfg-eth1   #修改其中的文件内容 ...

  9. sqlserver 批量修改表前缀

    先把第一句话放到sqlserver查询器中执行一下.然后把查询结果复制出来,进行编辑...一看你就懂了..简单的sql语句拼装 select ' exec sp_rename "' + na ...

  10. Unable to open connection to supplicant on "/data/misc/wifi/sockets/wlan0"

    在调试android wifi UI 的时候,出现了 logcat:  Unable to open connection to supplicant on "/data/misc/wifi ...