集训第十二天(2017/8/11):刷题(二分法)
来源:互联网 发布:现代通信网络试题 编辑:程序博客网 时间:2024/05/21 19:28
今天还是刷二分法的题,又做了一道莫名其妙的题,用scanf输入的话能ac,用cin输入就runtime error,我真的不知道这是什么情况??贴上这道题(通过这道题也收获了很多,比如说筛法求素数这道题,也是一道挺好的题),有没有大佬知道怎么解决?
Prime Gap
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 30 Accepted Submission(s) : 24
The sequence of n 1 consecutive composite numbers (positive integers that are not prime and not equal to 1) lying between two successive prime numbers p and p + nis called a prime gap of length n. For example, 24, 25, 26, 27, 28 between 23 and 29 is a prime gap of length 6.
Your mission is to write a program to calculate, for a given positive integer k, the length of the prime gap that contains k. For convenience, the length is considered 0 in case no prime gap contains k.
10112724921700
4060114
#include<iostream>
#include<cstdio>
using namespace std;
int prime[1300000],primenum[100010];
int isprime()//筛法求素数
{
for(int i=2;i<=1300000;i++)
prime[i]=1;
for(int i=2;i<=1300000;i++)
for(int j=i+i;j<=1300000;j+=i)
prime[j]=0;
int num=0;
for(int k=2;k<=1300000;k++)
{
if(prime[k]) primenum[num++]=k;
}
return num;
}
int main()
{
int num=isprime();
int n;
while(scanf("%d",&n)!=EOF && n)//while(cin>>n&&n)运行时错误?!
{
if(prime[n])
{
cout<<0<<endl;
continue;
}
else
for(int i=0;i<num;i++)
{
if(primenum[i]<n&&primenum[i+1]>n)
cout<<(primenum[i+1]-primenum[i])<<endl;
}
}
return 0;
}
- 集训第十二天(2017/8/11):刷题(二分法)
- 集训第十一天(2017/8/10):二分法练习和第四次做比赛
- 集训第十三天(2017/8/12)刷二分法的题
- 2017.8.11暑假集训第十二天
- 集训第二十六天(2017/8/25):集训总结
- 集训第九天(2017/8/8):刷题+做比赛
- 集训第二天(2017/8/1):第一次做比赛
- 集训第十六天(2017/8/15):树状数组
- 集训第二十天(2017/8/19)CCPC打酱油
- 第十二天(XML)
- 2017/8/4 第十二天
- 第十二天(使用类)
- 集训第五天(2017/8/4):刷搜索题
- 二分法(摘)
- 二分法(折半查找)
- 求解方程(二分法)
- 二分法(数组形式)
- Monthly Expense(二分法)
- 质数中的质数(质数筛法)||51NOD1181
- CodeForces
- Java 枚举类的基本使用
- Python框架——flask(2)
- CodeForces 1C
- 集训第十二天(2017/8/11):刷题(二分法)
- 圆圈中最后剩下的数字
- 深入了解selenium及webdriver原理
- javaweb在校期间需要完成的任务
- 河南省多校连萌(三)【问题 F: 小ho的01串】
- Android使用react-native-video 播放视频一直重复播放的解决方法
- Android中级
- 暴利链表 钟
- [第六季结尾]14.Javascript中的对象