codevs 1436 孪生素数 2

来源:互联网 发布:linux 启动网络服务 编辑:程序博客网 时间:2024/05/17 16:53

题目描述 Description
如m=100,n=6

则将输出100以内的所有相差6的孪生素数:如,

5 11

7 13

….

83 89

请按此规律输出数与数之间用半角空格区分,每一对一行.

输入描述 Input Description
第一行输入一个整数数m为一个范围(如100)

第二行输入一个整数k为目标孪生素数的公差(如6)

输出描述 Output Description
每行输出一对,最后一行输出:Total Is:?(?表示总共有几对这样的数,如果不存在则输出Total Is:0)

样例输入 Sample Input
例如1:

50 2

例如2:

100 90

例如3:

200 199

样例输出 Sample Output
例如1:

3 5
5 7
11 13
17 19
29 31
41 43
Total Is:6

例如2:

7 97
Total Is:1

例如3:

Total Is:0

数据范围及提示 Data Size & Hint
m<=5000

思路:数据比较水,直接暴力即可

代码如下

#include <iostream>  #include <cstdio>  #include <cstring>  #include <cmath>  #include <algorithm>#define LL long long using namespace std;  int solve(int x){    for(int i=2;i<=sqrt(x);i++)    if(x%i==0)    {        return 0;    }    return 1;}int main(){    int m,n;    scanf("%d%d",&m,&n);    int tol=0;    for(int i=2;i+n<=m;i++)    {        if(solve(i)&&solve(i+n))        {            printf("%d %d\n",i,i+n);            tol++;        }    }    printf("Total Is:%d",tol);}