Prime Gap

这里直接写中文了

Descriptions:

对于一个数n,若n为素数则输出0,否则找到距离n最小的两个素数,一个大于n,一个小于n,输出他们的差(正数)

Input

多组输入

每行包含一个数n

若n为0,程序结束

Output

对于每个测试数据,输出一个答案占一行

Sample Input

10

11

27

2

492170

0

Sample Output

4

0

6

0

114

题目链接:

https://vjudge.net/problem/UVA-1644

水题,先求质数表,预先处理出质数,然后输入n,若n为质数,输出0,若不为,可从第2个质数往后扫,当出现第一个大于n的数时,即可将他减去上一个质数,即为答案。

AC代码

#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define ME0(x) memset(x,0,sizeof(x))
using namespace std;
int T;
int n;
int isprime[];//判断这里面哪个数是素数
int ans[];//把素数全部存进一个数组,方便查找
void eratos(int x)//求素数表
{
for(int i=; i<=x; ++i)
isprime[i]=true;
isprime[]=isprime[]=false;
for(int i=; i<=x; ++i)
{
if(isprime[i])
{
int j=i+i;
while(j<=x)
{
isprime[j]=false;
j+=i;
}
}
}
}
void solve()
{
if(isprime[n])
{
cout<<<<endl;
return;
}
else
{
for(int i=; ;++i)
{
if(ans[i]<n&&ans[i+]>n)
{
cout<<ans[i+]-ans[i]<<endl;
return;
}
} }
}
int main()
{
int j=-;
eratos();
for(int i=; i<=; ++i)
if(isprime[i])
ans[++j]=i;
// for(int i=0; i<=10; ++i)
// cout<<ans[i]<<endl;
while(cin>>n,n)
{
solve();
}
}

【UVA - 1644 / POJ - 3518】Prime Gap(水题)的更多相关文章

  1. POJ 3518 Prime Gap(素数)

    POJ 3518 Prime Gap(素数) id=3518">http://poj.org/problem? id=3518 题意: 给你一个数.假设该数是素数就输出0. 否则输出比 ...

  2. POJ 3518 Prime Gap(素数题)

    [题意简述]:输入一个数,假设这个数是素数就输出0,假设不是素数就输出离它近期的两个素数的差值,叫做Prime Gap. [分析]:这题过得非常险.由于我是打的素数表. 由于最大的素数是1299709 ...

  3. poj 3518 Prime Gap

    Prime Gap Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7392   Accepted: 4291 Descrip ...

  4. UVa 1644 Prime Gap (水题,暴力)

    题意:给定一个数 n,求它后一个素数和前一个素数差. 析:先打表,再二分查找. 代码如下: #pragma comment(linker, "/STACK:1024000000,102400 ...

  5. POJ 1488 Tex Quotes --- 水题

    POJ 1488 题目大意:给定一篇文章,将它的左引号转成 ``(1的左边),右引号转成 ''(两个 ' ) 解题思路:水题,设置一个bool变量标记是左引号还是右引号即可 /* POJ 1488 T ...

  6. POJ 3641 Oulipo KMP 水题

    http://poj.org/problem?id=3461 直接KMP就好.水题 #include<cstdio> #include<cstring> const int M ...

  7. poj 1006:Biorhythms(水题,经典题,中国剩余定理)

    Biorhythms Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 110991   Accepted: 34541 Des ...

  8. poj 1002:487-3279(水题,提高题 / hash)

    487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 236746   Accepted: 41288 Descr ...

  9. poj 1003:Hangover(水题,数学模拟)

    Hangover Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 99450   Accepted: 48213 Descri ...

随机推荐

  1. jquery实现静态页面include

    最近在写一套前端页面,发现每个页面都复制一遍公共头.尾,十分不方便.于是就想能否也像PHP模板引擎一样,使用include引入公共文件,这便是接下来这段代码的意义.我们先来总结一下需求: 1.可以像使 ...

  2. ALIENTEK 战舰ENC28J60 LWIP和UIP补充例程(LWIP WEB有惊喜)

    前面的话:自从接触网络模块,到现在有一阵子时间了,未来必定是网络的世界.学一些网络方面的知识是有必要的.我们ALINTEK 推出的ENC28J60网络模块块作为入门还是不错的.详细见此贴:http:/ ...

  3. Acwing-166-数独(搜索)

    链接: https://www.acwing.com/problem/content/168/ 题意: 数独是一种传统益智游戏,你需要把一个9 × 9的数独补充完整,使得图中每行.每列.每个3 × 3 ...

  4. SpringMVC之RequestContextHolder分析(转)

    链接:https://blog.csdn.net/zzy7075/article/details/53559902

  5. 卸载python

    # rpm -qa|grep python|xargs rpm -ev --allmatches --nodeps # whereis python|xargs rm -frv

  6. 万能正则解析 json 数据 解析成键值对

    string txt = "{\"ip\": \"127.0.0.1\", \"port\": 80, \"status ...

  7. 洛谷比赛 U4858 sum

    U4858 sum 题目提供者666sb666 最新讨论 题目背景 定义一个序列的价值为序列中相邻元素差的绝对值之和. 如序列{2,1,3}的价值为|2-1|+|1-3|=3,而序列{4}的价值为0. ...

  8. java中如何根据函数查询引用的jar包

    选中函数,按Ctrl+Shift+T,就可以弹出对应的jar包地址 例如:

  9. BZOJ刷题列表【转载于hzwer】

    沿着黄学长的步伐~~ 红色为已刷,黑色为未刷,看我多久能搞完吧... Update on 7.26 :之前咕了好久...(足见博主的flag是多么emmm......)这几天开始会抽时间刷的,每天几道 ...

  10. Vue使用Axios实现http请求以及解决跨域问题

    Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中.Axios的中文文档以及github地址如下: 中文:https://www.kancloud.cn/y ...