代码只有40分,求大佬们出手相助

5. 素数对

题目ID:6367必做题100分

最新提交:

Time Limit Exceeded

40 分

历史最高:

Time Limit Exceeded

40 分

时间限制: 1000ms

空间限制: 524288kB

题目描述

两个相差为2的素数称为素数对,如5和7,17和19等,本题目要求找出所有两个数均不大于n的素数对。

输入格式

一个正整数n。

输出格式

所有小于等于n的素数对。每对素数对输出一行,中间用单个空格隔开。若没有找到任何素数对,输出empty。

样例

Input 1

100

Output 1

3 5 5 7 11 13 17 19 29 31 41 43 59 61 71 73

Input 2

2

Output 2

empty

样例解释

输入100时,小于等于100的素数对有8对,所以输出8行;输入2时,没有小于等于2的素数对,所以输出empty。

数据范围

1≤n≤10000

#include<bits/stdc++.h>
using namespace std;

int main(){
int n,s=0;
cin>>n;
for(int i=3;i<n;i+=2)
{
s+=i;
for(int a=2;a<s;a++)
{
if(s%a==0)
{
continue;
}
else
{
if((s+2)%a==0)
{
cout<<s<<" "<<s+2<<endl;
}
else
{
continue;
}
}
}
}
if(n<=4)
{
cout<<“empty”;
}
return 0;
}

3 个赞

(帖子已被作者删除)

1 个赞

(帖子已被作者删除)

1 个赞

求了,大佬们救救我吧

1 个赞
bool check(int x)
{
	for(int i=2;i*i<=x;i++)
	{
		if(x%i==0)return 0;
	}
	return 1; 
}

判断是否为质数的函数

for(int i=1;i<=n;i++)
{
	if(check(i)&&check(i+2))
	...... 
}

查找

2 个赞

谢谢

2 个赞