zoukankan      html  css  js  c++  java
  • luogu P1217 回文质数 枚举

    枚举出所有的的回文数,然后判断其是否在范围内,是否是素数即可。

     1 #include <cstdio>
     2 #include <cmath>
     3 #include <algorithm>
     4 using namespace std;
     5 int a,b,tot;
     6 int res[501000];
     7 bool ok(int x)
     8 {
     9     if (x < a || x > b)
    10         return false;
    11     int t = sqrt(x);
    12     for (int i = 2;i <= t;i++)
    13         if (x % i == 0)
    14             return false;
    15     return true;
    16 }
    17 int main()
    18 {
    19     scanf("%d%d",&a,&b);
    20     //长度为9回文数 
    21     for (int d1 = 1;d1 <= 9;d1 += 2)
    22         for (int  d2 = 0;d2 <= 9;d2++)
    23             for (int d3 = 0;d3 <= 9;d3++)
    24                 for (int d4 = 0;d4 <= 9;d4++)
    25                     for (int d5 = 0;d5 <= 9;d5++)
    26                     {
    27                         int t = d1 * 100000000 + d2 * 10000000 + d3 * 1000000 + d4 * 100000 + d5 * 10000 + d4 * 1000 + d3 * 100 + d2 * 10 + d1;
    28                         if (ok(t))
    29                             res[++tot] = t; 
    30                     }
    31     //长度为8的回文数
    32      for (int d1 = 1;d1 <= 9;d1 += 2)
    33         for (int  d2 = 0;d2 <= 9;d2++)
    34             for (int d3 = 0;d3 <= 9;d3++)
    35                 for (int d4 = 0;d4 <= 9;d4++)
    36                     {
    37                         int t = d1 * 10000000 + d2 * 1000000 + d3 * 100000 + d4 * 10000 + d4 * 1000 + d3 * 100 + d2 * 10 + d1;
    38                         if (ok(t))
    39                             res[++tot] = t; 
    40                     }
    41     //长度为7的回文数 
    42     for (int d1 = 1;d1 <= 9;d1 += 2)
    43         for (int  d2 = 0;d2 <= 9;d2++)
    44             for (int d3 = 0;d3 <= 9;d3++)
    45                 for (int d4 = 0;d4 <= 9;d4++)
    46                     {
    47                         int t = d1 * 1000000 + d2 * 100000 + d3 * 10000 + d4 * 1000 + d3 * 100 + d2 * 10 + d1;
    48                         if (ok(t))
    49                             res[++tot] = t; 
    50                     }
    51     //长度为6的回文数 
    52     for (int d1 = 1;d1 <= 9;d1 += 2)
    53         for (int  d2 = 0;d2 <= 9;d2++)
    54             for (int d3 = 0;d3 <= 9;d3++)
    55                 {
    56                     int t = d1 * 100000 + d2 * 10000 + d3 * 1000 + d3 * 100 + d2 * 10 + d1;
    57                     if (ok(t))
    58                         res[++tot] = t; 
    59                 }
    60     //长度为5的回文数 
    61     for (int d1 = 1;d1 <= 9;d1 += 2)
    62         for (int  d2 = 0;d2 <= 9;d2++)
    63             for (int d3 = 0;d3 <= 9;d3++)
    64                 {
    65                     int t = d1 * 10000 + d2 * 1000 + d3 * 100 + d2 * 10 + d1;
    66                     if (ok(t))
    67                         res[++tot] = t; 
    68                 }
    69     //长度为4的回文数 
    70     for (int d1 = 1;d1 <= 9;d1 += 2)
    71         for (int  d2 = 0;d2 <= 9;d2++)
    72             {
    73                 int t = d1 * 1000 + d2 * 100 + d2 * 10 + d1;
    74                 if (ok(t))
    75                     res[++tot] = t; 
    76             }
    77     //长度为3的回文数 
    78     for (int d1 = 1;d1 <= 9;d1 += 2)
    79         for (int  d2 = 0;d2 <= 9;d2++)
    80             {
    81                 int t = d1 * 100 + d2 * 10 + d1;
    82                 if (ok(t))
    83                     res[++tot] = t; 
    84             }
    85     //长度为2的回文数 
    86     for (int d1 = 1;d1 <= 9;d1 += 2)
    87         if (ok(d1 * 10 + d1))
    88             res[++tot] = d1 * 10 + d1;
    89     //长度为1的回文数 
    90     for (int d1 = 1;d1 <= 9;d1 += 2)
    91         if (ok(d1))
    92             res[++tot] = d1;
    93     sort(res + 1,res + tot + 1);
    94     for (int i = 1;i <= tot;i++)
    95         printf("%d
    ",res[i]);
    96     return 0;
    97 }
    心之所动 且就随缘去吧
  • 相关阅读:
    [Vue] Create Filters in Vue.js
    [Vue] Import component into page
    [Angular Form] ngModel and ngModelChange
    [Ramda] Convert a QueryString to an Object using Function Composition in Ramda
    [Vue] Use basic event handling in Vue
    [Ramda] Declaratively Map Data Transformations to Object Properties Using Ramda evolve
    Linux2.6内核--VFS层中和进程相关的数据结构
    [置顶] Firefox OS 学习——Gaia 编译分析
    ORACLE 索引概述
    【笔试&面试】C#的托管代码与非托管代码
  • 原文地址:https://www.cnblogs.com/iat14/p/11180973.html
Copyright © 2011-2022 走看看