UVA10394 Twin Primes【孪生素数】

Twin primes are pairs of primes of the form (p, p + 2). The term “twin prime” was coined by Paul Stckel (1892-1919). The first few twin primes are (3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43). In this problem you are asked to find out the S-th twin prime pair where S is an integer that will be given in the input.
Input
The input will contain less than 10001 lines of input. Each line contains an integers S (1 ≤ S ≤ 100000), which is the serial number of a twin prime pair. Input file is terminated by end of file.
Output
For each line of input you will have to produce one line of output which contains the S-th twin prime pair. The pair is printed in the form (p1,¡space¿p2). Here ¡space¿ means the space character (ASCII 32) . You can safely assume that the primes in the 100000-th twin prime pair are less than 20000000.
Sample Input
1
2
3
4
Sample Output
(3, 5)
(5, 7)
(11, 13)
(17, 19)

问题链接UVA10394 Twin Primes
问题简述:(略)
问题分析
    孪生素数问题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10394 Twin Primes */

#include <bits/stdc++.h>

using namespace std;

const int N = 20000000;
bool isprime[N + 1];
int prime[N / 3], pcnt = 0, twprime[N / 9], twcnt = 1;
// 欧拉筛
void eulersieve(void)
{
    memset(isprime, true, sizeof(isprime));

    isprime[0] = isprime[1] = false;
    for(int i = 2; i <= N; i++) {
        if(isprime[i])
            prime[pcnt++] = i;
        for(int j = 0; j < pcnt && i * prime[j] <= N; j++) {  //筛选
            isprime[i * prime[j]] = false;
            if(i % prime[j] == 0) break;
        }
    }

    prime[pcnt] = 0;
    for(int i = 0; i < pcnt; i++)
        if(prime[i] + 2 == prime[i + 1]) twprime[twcnt++] = prime[i];
 }

int main()
{
    eulersieve();

    int n;
    while(~scanf("%d", &n))
        printf("(%d, %d)\n", twprime[n], twprime[n] + 2);

    return 0;
}
发布了2126 篇原创文章 · 获赞 2306 · 访问量 254万+

猜你喜欢

转载自blog.csdn.net/tigerisland45/article/details/104544397