南邮 OJ 1386 Primes

Primes

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

比赛描述

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





#include<iostream>
#define MAX_N 1000
#define PAIR 15

bool isPrime[MAX_N];
int a[(PAIR+1)*2];

int main(){
	int i,j;
	for(i=2;i<MAX_N;i++){
		isPrime[i] = 1;
	}
	for(i=4;i<MAX_N;i+=2){
		isPrime[i] = 0;
	}
	for(i=3;i<MAX_N;i+=2){
		if(isPrime[i]){
			for(j=(i<<1);j<MAX_N;j+=i){
				isPrime[j] = 0;
			}
		}
	}
	for(i=0,j=3;i<PAIR*2;j++){
		if(isPrime[j] && isPrime[j+2]){
			a[i++] = j;
			a[i++] = j+2;
		}
	}
	for(i=2*PAIR-1;i>=1;i-=2){
		printf("(%d,%d)\n",a[i-1],a[i]);
	}
}






你可能感兴趣的:(ACM,primes,南邮OJ)