zoukankan      html  css  js  c++  java
  • 1013 数素数 (20 分)

    Pi​​ 表示第 i 个素数。现任给两个正整数 MN104​​,请输出 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

    分析:这个题注意给的数字是第几个素数,而不是从M到N中找素数,再有就是格式问题。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cmath>
     5 using namespace std;
     6 int main()
     7 {
     8     int x,y;
     9     cin>>x>>y;
    10     int s=0;
    11     int k=0,a[10000];
    12     for(int i=2;k<=y;i++)
    13     {
    14         int flag=0;
    15         for(int j=2;j<=sqrt((double)i);j++)
    16         {
    17             if(i%j==0)
    18             {
    19                 flag++;
    20                 break;
    21             }
    22         }
    23         if(!flag)
    24            a[k++]=i;
    25     }
    26     for(int i=x-1;i<y;i++)
    27     {
    28         s++;
    29         if(s==10)
    30         {
    31             cout<<a[i]<<endl;
    32             s=0;
    33         }
    34         else if(i!=y-1)
    35             cout<<a[i]<<' ';
    36             else
    37                 cout<<a[i];
    38     }
    39 }
  • 相关阅读:
    [POJ 1050]To the Max
    P1678 烦恼的高考志愿
    P1873 砍树
    P1102 A-B 数对
    P6771 [USACO05MAR]Space Elevator 太空电梯
    P2347 砝码称重
    P1832 A+B Problem(再升级)
    P1679 神奇的四次方数
    P1877 [HAOI2012]音量调节
    P1049 装箱问题
  • 原文地址:https://www.cnblogs.com/ygjojo/p/10742274.html
Copyright © 2011-2022 走看看