zoukankan      html  css  js  c++  java
  • 10:素数对

    10:素数对

    总时间限制: 
    1000ms
     
    内存限制: 
    65536kB
    描述

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

    输入
    一个正整数n。1 <= n <= 10000。
    输出
    所有小于等于n的素数对。每对素数对输出一行,中间用单个空格隔开。若没有找到任何素数对,输出empty。
    样例输入
    100
    样例输出
    3 5
    5 7
    11 13
    17 19
    29 31
    41 43
    59 61
    71 73

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 using namespace std;
     5 int b[10001];
     6 int now;
     7 int n;
     8 void find(int a)
     9 {
    10     for(int i=3;i<=a;i++)
    11     {
    12         for(int j=2;j<i;j++)
    13         {
    14             if(i%j==0)
    15             break;
    16             else if(j==i-1&&i%j!=0)
    17             {
    18                 b[now]=i;
    19                 now++;
    20                 break;
    21             }
    22         }
    23     }
    24     for(int i=0;i<now;i++)
    25     {
    26         if(b[i+1]-b[i]==2)
    27         {
    28             cout<<b[i]<<" "<<b[i+1];
    29             cout<<endl;    
    30         }
    31     }
    32 }
    33 int main() 
    34 {
    35     
    36     cin>>n;
    37     if(n<5)cout<<"empty";
    38     else 
    39     find(n);
    40     return 0;
    41 }
  • 相关阅读:
    Lucky Coins Sequence
    A == B ?
    Chinese Rings
    51nod 1051 最大子矩阵和
    51nod 1103 N的倍数
    Codeforces Round #429 (Div. 2)
    51nod 1043 幸运号码(数位dp
    51nod 1266 蚂蚁
    51nod 1090 3个数和为0
    51nod 1082 与7无关的数
  • 原文地址:https://www.cnblogs.com/zwfymqz/p/6498298.html
Copyright © 2011-2022 走看看