1013. 数素数 (20)
时间限制
100 ms
内存限制
32000 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
令Pi表示第i个素数。现任给两个正整数M <= N <= 104,请输出PM到PN的所有素数。
输入格式:
输入在一行中给出M和N,其间以空格分隔。
输出格式:
输出从PM到PN的所有素数,每10个数字占1行,其间以空格分隔,但行末不得有多余空格。
输入样例:5 27输出样例:
11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103
/* http://pat.zju.edu.cn/contests/pat-b-practise/1013 1013. 数素数 (20) */ #include<cmath> #include<iostream> using namespace std; bool isPrime(int a) { if(a==1 || a==0) return false; if(a==2) return true; if(a%2 == 0) return false; int temp = (int)sqrt((double)a); for(int i = 3;i<=temp;i++) if(a%i ==0 ) return false; return true; } int main() { int left,right; cin>>left>>right; int i = 1; int count = 0;//计数素数 while(1) { i++; if(isPrime(i)) count++; if(count==left) break; } while(count<right) { cout<<i; if((count-left)%10!=9) cout<<" "; i++; while(!isPrime(i)) i++; count++; if((count-left)%10==0) cout<<endl; } cout<<i; system("pause"); return 0; }