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

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. EditText光标居上

    <EditText android:id="@+id/shareContent_editText" android:layout_width="wrap_conte ...

  2. 在 Linux 环境下报错 java.lang.reflect.InvocationTargetException

    今天开发了一个 excel 导出数据的功能,放到 linux 服务器上后发现报错. 捕获到 java.lang.reflect.InvocationTargetException 异常,这个异常不太常 ...

  3. leetcode342合理运用位操作判断4的幂

    Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Gi ...

  4. Jersey客户端API调用REST风格的Web服务

    Jersey 客户端 API 基础 jersey-1.14.jar 密码: cxug 要开始使用 Jersey 客户端 API,你首先需要创建一个 com.sun.jersey .api.client ...

  5. 第三十六节,os系统级别操作模块

    在使用os模块时需要先 import os 引入模块 os.getcwd()模块函数 功能:获取当前工作目录,即当前python脚本工作的目录路径[无参] 使用方法:os.getcwd() 格式如:a ...

  6. hdu_1115_Lifting the Stone(求多边形重心)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1115 题意:给你N个点围成的一个多边形,让你求这个多边形的重心. 题解: 将多边形划分为若干个三角形. ...

  7. FUSE and File System

    FUSE: File system in USErspace. So what is a file system? A file system maps file paths to file cont ...

  8. Java良葛格 学习笔记

    学习一个新的事物时,如果遇到一些概念无法很快理解,这可能是因为要理解概念会需要其它概念先建立起来,所以先暂时放下这个疑问也是一个学习方法,称之为“存疑” ,在以后的学习过程中待必要的概念学会后,目前的 ...

  9. tableView滚动的时候会 最后一行显示不完全的问题

    问题可能原因 1:tableView高度的设置不正确,应该是屏幕的高度减去上面的高度(包括状态栏以及navigationBar的高度).正确设置了tableView的高度之后,才可以正常滚动到最后一行 ...

  10. Ajax及select级联

    cascade.jsp(Test/WebContent/jsp/cascade.jsp): <%@ page language="java" contentType=&quo ...