HDOJ 1319 Prime Cuts<数论>
学会了不难。通过这道题学习了两点:
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<数论>的更多相关文章
- poj 1595 Prime Cuts
Prime Cuts Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10610 Accepted: 4046 Descr ...
- POJ1595 Prime Cuts
Prime Cuts Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11961 Accepted: 4553 Descr ...
- [暑假集训--数论]poj1595 Prime Cuts
A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In ...
- 【HDU】2866:Special Prime【数论】
Special Prime Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- hdoj 1016 Prime Ring Problem
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, ... ...
- 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 ...
- HDOJ 1016 Prime Ring Problem素数环【深搜】
Problem Description A ring is compose of n circles as shown in diagram. Put natural number 1, 2, -, ...
- UVA 10140 - Prime Distance(数论)
10140 - Prime Distance 题目链接 题意:求[l,r]区间内近期和最远的素数对. 思路:素数打表,打到sqrt(Max)就可以,然后利用大的表去筛素数.因为[l, r]最多100W ...
- Codefoces 432C Prime Swaps(数论+贪心)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u011328934/article/details/26094917 题目连接:Codefoces ...
随机推荐
- WINDOWS特有的消息常量标识符
'========================================'WINDOWS特有的消息常量标识符'======================================== ...
- Chapter 2 Open Book——14
I backpedaled. "They seemed nice enough to me. I just noticed they keptto themselves. 我改口说道,他们看 ...
- leetcode136 利用异或运算找不同的元素
Given an array of integers, every element appears twice except for one. Find that single one. Note: ...
- 网页特效-动态加载JavaScript
描述: 把一些逻辑独立的JavaScript脚本文件单独加载,是一种常见的JavaScript动态加载技术.可以减少不必要的JavaScript脚本文件的加载,以提高网页浏览速度 代码: <!D ...
- spark 编程向导
http://spark.apache.org/docs/latest/programming-guide.html
- LeetCode OJ 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- [实用]DNS解析命令,静静地学会【转载】
[实用]DNS解析命令,静静地学会 2016-08-04 06:50 一.Windows下的nslookup 简单的查某个域名,那就nslookup toutiao.com,上面是dns地址,下面是解 ...
- mynotebook
www.linux.org/threads/beats-audio-on-linux.4443/ askubuntu.com/questions/303775/envy-15-beats-audio- ...
- label自适应
//label自适应 self.label = [UILabel new]; self.label.font = [UIFont systemFontOfSize:14]; NSString *tit ...
- Form类的KeyPreview属性
首先需要知道一个知识点,Form控件,Panel控件和GroupBox控件等容器类控件默认是不接收焦点的,而是负责管理容器中控件的焦点.当容器控件被选中时,默认把焦点传送至容器内Tab顺序为0的控件. ...