zoukankan      html  css  js  c++  java
  • NOJ Primes

    Primes

    时间限制(普通/Java):1000MS/3000MS         运行内存限制:65536KByte
    总提交:75          测试通过:42

    题目描述

    Wikipedia says: A twin prime is a prime number that differs from another prime number by two. Except for

    the pair (2, 3), this is the smallest possible difference between two primes. Some examples of twin prime

    pairs are (3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43),…

    Write a program that generates the first 15 pairs of twin primes, beginning with (3,5). Display the pairs in

    reverse order, so the last pair to be displayed will be (3,5).


    输入

    undefined

    输出

    Display each pair of values on a separate line, surrounded by parentheses and separated by a comma.

    Examples are:

    (17,19)

    (11,13)

    1386 H_PRC3


    样例输入

    样例输出

    提示

    undefined

    题目来源

    Internet


    分析:首先进行素数预处理,判断相邻的两个素数差值是否为2,如果满足进行保存,倒序输出。


    实现代码:

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    int vis[100000],prime[100000];
    int cnt=0;
    int main()
    {
        memset(vis,0,sizeof(vis));
        for(int i=2;i<=10000;i++)
        {
            if(!vis[i])
               prime[cnt++]=i;
            for(int j=i;j<=10000;j+=i)
                vis[j]=1;
        }
        int num=0,left[16],right[16];
        memset(left,0,sizeof(left));
        memset(right,0,sizeof(right));
        for(int i=1;i<cnt&&num<15;i++)
        {
            if(prime[i-1]+2==prime[i])
            {
                  left[num]=prime[i-1];
                  right[num]=prime[i];
                  num++;
            }
        }
        for(int i=num-1;i>=0;i--)
        {
            printf("(%d,%d)
    ",left[i],right[i]);
        }
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Javascript事件处理程序的3种方式
    JS原生AJAX
    所谓的渐进增强,优雅降级?
    1059 老师的苦恼
    HTML5 参数传递
    HDU 5289 Assignment(二分+RMQ-ST)
    HDU 3333 Turing Tree(离线树状数组)
    mac下 mysql 插入中文乱码解决
    校园商铺-7商品类别模块-5商品类别删除后端开发
    校园商铺-7商品类别模块-3商品类别添加后端开发
  • 原文地址:https://www.cnblogs.com/Tobyuyu/p/4965463.html
Copyright © 2011-2022 走看看