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

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. HDU 3338 Kakuro Extension

    网络最大流 TLE了两天的题目.80次Submit才AC,发现是刘汝佳白书的Dinic代码还可以优化.....瞬间无语..... #include<cstdio> #include< ...

  2. Qt - 设置TableWidget只读

    ui->infoViewTW->setEditTriggers(QAbstractItemView::NoEditTriggers); enum QAbstractItemView::Ed ...

  3. 编译使用luasocket

    编译lua5.1: 因为luasocket使用的是lua5.1,所以先下载lua5.1,编译,并把头文件和dll放在xxx/lua5.1/include和xxx/lua5.1/lib 编译luasoc ...

  4. PHPExcel解决内存占用过大问题-dw 查找memoryCacheSize把1M改为2048M

    http://blog.sina.com.cn/s/blog_4ec7952d0101fcrd.html PHPExcel解决内存占用过大问题-设置单元格对象缓存 PHPExcel是一个很强大的处理E ...

  5. VS中的快捷键快速格式化代码,使好看,整齐

    在VC2005中,快捷键是Ctrl + K, Ctrl + F, 这是一个组合键,即先按Ctrl + K, 这时候编辑器会等待下一个按键动作,此时再按Ctrl + F, 即可以格式化代码了,当然,也可 ...

  6. ice使用过程遇到的问题

    1 设置代理超时时间ice_timeout ICE的每个连接都有两个超时时间:ice_timeout.ice_connectiontimeout,分别对应消息的超时时间和连接建立    的超时时间,可 ...

  7. ios做的两个矩形相交叉

    #import "ViewController.h" @interface ViewController (){    UIView *_gee;  //定义的实例变量    UI ...

  8. ecshop后台管理显示扩展分类

    ecshop 后台商品列表默认只显示分类下的商品,而不显示扩展分类中的商品,以下是我个人给出的解决方法: 打开admin/includes/lib_goods.php 第839行左右的位置 可以看到如 ...

  9. Hibernate Tools

    (声明)本文转自:http://linjia880714.iteye.com/blog/859334 hibernate-tools详细使用教程 使用hibernate-tool的版本是hiberna ...

  10. C++字符串(1)

    C++ 拼接字符串常量 C++允许拼接字符串字面值,即将两个用引号括起的字符串合并为一个.事实上,任何两个由空白(空格,制表符和换行符)分隔的字符串常量都将自动拼接成一个. 例子: cout < ...