PAT 1013. 数素数 (20)

来源:互联网 发布:mysql数据库查询工具 编辑:程序博客网 时间:2024/06/08 00:06

令Pi表示第i个素数。现任给两个正整数M <= N <= 104,请输出PM到PN的所有素数。

输入格式:

输入在一行中给出M和N,其间以空格分隔。

输出格式:

输出从PM到PN的所有素数,每10个数字占1行,其间以空格分隔,但行末不得有多余空格。

输入样例:
5 27
输出样例:
11 13 17 19 23 29 31 37 41 4347 53 59 61 67 71 73 79 83 8997 101 103
问题分析
(1)和之前一题素数的类似,将所有素数存入一个数组即可。
#include <iostream>#include <cmath>using namespace std;int main (){int M;int N;cin>>M;cin>>N;int outcome[10000]={0};   int k=0;int temp=1;while ((k-1)!=N)    { int mark=0;         for (int j=1;j<=sqrt(temp);j++) { if (temp%j==0&&j>1) {mark++; break;} } if (mark==0) {outcome[k]=temp; if (k>=M) {         if ((k-M)%10==0)cout<<outcome[k];else if ((k-M)%10==9)cout<<' '<<outcome[k]<<endl;else             cout<<' '<<outcome[k]; }   k++;} temp++;}return 0;}


0 0
原创粉丝点击