POJ 3518 Prime Gap(素数题)
【题意简述】:输入一个数,假设这个数是素数就输出0,假设不是素数就输出离它近期的两个素数的差值,叫做Prime Gap。
【分析】:这题过得非常险。由于我是打的素数表。
由于最大的素数是1299709,所以注意在打表时要使用long long。否则程序应该不能执行。注意这一点应该就能够了。
积累!
// 2984K 235Ms
#include<iostream>
using namespace std;
#define N 2000001 bool isprime[N];
long long prime[100001],nprime; // 注意long long void doprime()
{
long long i,j; // !!注意
nprime = 1;
memset(isprime,true,sizeof(isprime));
isprime[1] = 0;
for(i = 2;i<=1299709;i++)
{
if(isprime[i])
{
prime[nprime++] = i;
for(j = i*i;j<=1299709;j+=i)
{
isprime[j] = false;
}
}
}
} int main()
{
long long n;//注意。
doprime();
while(1)
{
cin>>n;
if(n == 0)
break; for(long long i = 0;i<nprime;i++)
{
if(n == prime[i]){
cout<<0<<endl;
break;
} if(n>prime[i]&&n<prime[i+1]){
cout<<prime[i+1]-prime[i]<<endl;
break;
}
}
}
return 0;
}
POJ 3518 Prime Gap(素数题)的更多相关文章
- POJ 3518 Prime Gap(素数)
POJ 3518 Prime Gap(素数) id=3518">http://poj.org/problem? id=3518 题意: 给你一个数.假设该数是素数就输出0. 否则输出比 ...
- poj 3518 Prime Gap
Prime Gap Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7392 Accepted: 4291 Descrip ...
- POJ 3581 Prime Gap(二分)
Prime Gap Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11009 Accepted: 6298 Descriptio ...
- POJ 3126 Prime Path 素数筛,bfs
题目: http://poj.org/problem?id=3126 困得不行了,没想到敲完一遍直接就A了,16ms,debug环节都没进行.人品啊. #include <stdio.h> ...
- POJ - 3126 Prime Path 素数筛选+BFS
Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Security s ...
- UVa 1644 Prime Gap (水题,暴力)
题意:给定一个数 n,求它后一个素数和前一个素数差. 析:先打表,再二分查找. 代码如下: #pragma comment(linker, "/STACK:1024000000,102400 ...
- 【UVA - 1644 / POJ - 3518】Prime Gap(水题)
Prime Gap 这里直接写中文了 Descriptions: 对于一个数n,若n为素数则输出0,否则找到距离n最小的两个素数,一个大于n,一个小于n,输出他们的差(正数) Input 多组输入 每 ...
- poj 2689 Prime Distance(大区间素数)
题目链接:poj 2689 Prime Distance 题意: 给你一个很大的区间(区间差不超过100w),让你找出这个区间的相邻最大和最小的两对素数 题解: 正向去找这个区间的素数会超时,我们考虑 ...
- POJ 3126 Prime Path(素数路径)
POJ 3126 Prime Path(素数路径) Time Limit: 1000MS Memory Limit: 65536K Description - 题目描述 The minister ...
随机推荐
- iOS解析数据判断nil NULL Null的方法
+ (BOOL)isNil:(NSObject*)obj { if (obj == nil || obj == NULL) { return YES; } if ([obj isKindOfClass ...
- [Swust OJ 360]--加分二叉树(区间dp)
题目链接:http://acm.swust.edu.cn/problem/360/ Time limit(ms): 1000 Memory limit(kb): 65535 Description ...
- 10min系列之二日志可视化进阶
10min系列之二日志可视化进阶(作者原创,同步发布在github) 本文需要有一定的python和前端基础,如果没基础的,请关注我后续的基础教程系列博客 本文所有的demo,都是浏览器下展示的 原创 ...
- Storm几篇文章
http://tianhailong.com/ http://www.cnblogs.com/panfeng412/archive/2012/07/02/storm-common-patterns-o ...
- Android自定义控件(36篇)
http://blog.csdn.net/lmj623565791/article/details/44278417 http://download.csdn.net/user/lmj62356579 ...
- Windows 8.1 IIS 8.5 远程管理 Windows 2008 R2 IIS 7.0
案例: Windows 8.1 x64 IIS 8.5 inetmgr_amd64_v1.1_en-US.msi Windows 2008 R2 x64 IIS 7.0 在Win8.1 通过IIS ...
- Tomcat+redis+nginx配置
为客户开发的一个绩效系统,采用了java web的开发方式,使用了一些spring mvc, mybatis之类的框架.相比于oracle ebs的二次开发,这种开发更加灵活,虽然和ebs集成的时候遇 ...
- Linux Centos 系统上安装BT客户端 Transmission
Linux Centos 系统上安装BT客户端 Transmission Transmission是一种BitTorrent客户端,特点是一个跨平台的后端和其上的简洁的用户界面,以MIT许可证和G ...
- Windows Azure HDInsight 支持预览版 Hadoop 2.2 群集
Windows Azure HDInsight 支持预览版 Hadoop 2.2 群集 继去年 10 月推出 Windows Azure HDInsight 之后,我们宣布 Windows Az ...
- 动态链接库dll,静态链接库lib, 导入库lib
转载地址:http://www.cnblogs.com/chio/archive/2008/08/05/1261296.html 目前以lib后缀的库有两种,一种为静态链接库(Static Libar ...