标签:
原题:
Prime GeneratorProblem code: PRIME1 |
Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers!
The input begins with the number t of test cases in a single line (t<=10). In each of the next t lines there are two numbers m and n (1 <= m <= n <= 1000000000, n-m<=100000) separated by a space.
For every test case print all prime numbers p such that m <= p <= n, one number per line, test cases separated by an empty line.
Input: 2 1 10 3 5 Output: 2 3 5 7 3 5
这题太水,我也不知道为什么,ac率会这么低,我用最简单的方法竟然过了,本来以为要用到筛法呢,用兴趣的可以看看晒法。不说了,直接上代码:
1 #include<iostream> 2 #include<stdio.h> 3 #include<cmath> 4 using namespace std; 5 6 int isPrime(int n) 7 { 8 if(n<=1) 9 { 10 return false; 11 } 12 if(n==2) 13 { 14 return true; 15 } 16 else 17 { 18 for(int i=2; i<=sqrt(n); i++) 19 { 20 if(n%i==0) 21 return false; 22 } 23 return true; 24 } 25 } 26 27 int main() 28 { 29 int t; 30 int m,n; 31 scanf("%d",&t); 32 while(t--) 33 { 34 scanf("%d%d",&m,&n); 35 for(int j=m; j<=n; j++) 36 { 37 if(isPrime(j)) 38 printf("%d\n",j); 39 } 40 printf("\n"); 41 } 42 return 0; 43 }
标签:
原文地址:http://www.cnblogs.com/leetao94/p/4175474.html