• poj1595 水题


     题意:输入n, 和c
     统计1 - n 有多少个素数为cnt
     若 2*c > cnt 则将素数全部输出
     否则分支判断:
      若cnt 为偶数,则从中心开始输出2*c 个
      若cnt 为奇数,则从中心开始输出2*c-1个

     1 /*
     2     题意:输入n, 和c 
     3     统计1 - n 有多少个素数为cnt
     4     若 2*c > cnt 则将素数全部输出
     5     否则分支判断:
     6         若cnt 为偶数,则从中心开始输出2*c 个
     7         若cnt 为奇数,则从中心开始输出2*c-1个
     8 */
     9 #include <iostream>
    10 using namespace std;
    11 bool prm[1005];    //全局为false
    12 void Prime()
    13 {
    14     int i, t;
    15     prm[1] = false;
    16     for (i=2; i<1005; i++)
    17     {
    18         if (!prm[i])
    19         {
    20             t = i<<1;
    21             while (t<1005)
    22             {
    23                 prm[t] = true;
    24                 t+= i;
    25             }
    26         }
    27     }
    28 }
    29 int main()
    30 {
    31     int n, c, i, j, cnt;
    32     Prime();
    33     while (cin>>n>>c)
    34     {
    35         cout<<n<<" "<<c<<":";
    36         for (i=1, cnt=0; i<=n; i++)
    37         {    
    38             if (!prm[i])
    39                 cnt++;
    40         }
    41         //for (i=1; i<=n; i++)
    42         //        if (!prm[i])
    43         //            cout<<i<<" ";
    44         //    cout<<endl;
    45         if (2*c > cnt)
    46         {
    47             for (i=1; i<=n; i++)
    48                 if (!prm[i])
    49                     cout<<" "<<i;
    50             cout<<endl;
    51             /*continue;*/
    52         }
    53         else
    54         {
    55             if (cnt % 2 == 0)
    56             {
    57                 for (j=1, i=1; j<(cnt-2*c)/2+1;i++)
    58                 {
    59                     if (!prm[i])
    60                         j++;
    61                 }
    62                 for (j=0; j<2*c; i++)
    63                     if (!prm[i])
    64                     {
    65                         j++;
    66                         cout<<" "<<i;
    67                     }
    68                 cout<<endl;
    69             }
    70             else
    71             {
    72                 for (j=1, i=1; j<(cnt-2*c+1)/2+1;i++)
    73                 {
    74                     if (!prm[i])
    75                         j++;
    76                 }
    77                 for (j=1; j<2*c; i++)
    78                     if (!prm[i])
    79                     {
    80                         j++;
    81                         cout<<" "<<i;
    82                     }
    83                 cout<<endl;
    84             }
    85         }
    86         cout<<endl;
    87     }
    88     return 0;
    89 }
    View Code
  • 相关阅读:
    echars 折线图使用
    echars 饼图使用
    React native中DrawerNavigator,StackNavigator,TabNavigator导航栏使用
    React native 中 SectionList用法
    React native 中使用Fetch请求数据
    React native中使用XMLHttpRequest请求数据
    实现在WebView中返回上一级
    DatePickerAndroid用法
    Lambda
    .Net常见线程安全问题整理
  • 原文地址:https://www.cnblogs.com/lv-2012/p/3208607.html
Copyright © 2020-2023  润新知