zoukankan      html  css  js  c++  java
  • USACO Section 1.5 Prime Palindromes

    直接用最笨的方法。。。

    ANALYSIS里有讲,其实只算一半长度就行

    代码相当的难看。。。

      1 /*
    2 ID:linyvxi1
    3 PROB:pprime
    4 LANG:C++
    5 */
    6 #include <stdio.h>
    7 #include <math.h>
    8 void do_1();
    9 void do_2();
    10 void do_3();
    11 void do_4();
    12 void do_5();
    13 void do_6();
    14 void do_7();
    15 void do_8();
    16 inline bool prime(int n)
    17 {
    18 int i;
    19 for(i=2;i<=(int)sqrt(n)+1;i++){
    20 if(n%i==0)
    21 return false;
    22 }
    23 return true;
    24 }
    25 int left,right,min_len,max_len;
    26 void get_len()
    27 {
    28 min_len=0;
    29 max_len=0;
    30 int temp1=left;
    31 int temp2=right;
    32 while(left){
    33 min_len++;
    34 left/=10;
    35 }
    36 while(right){
    37 max_len++;
    38 right/=10;
    39 }
    40 left=temp1;
    41 right=temp2;
    42 }
    43 void do_1(){
    44 int cur_len=1;
    45 int num1;
    46 int palindromes;
    47 for(num1=1;num1<=9;num1++){
    48 palindromes=num1;
    49 if(palindromes<left||!(prime(palindromes)))
    50 continue;
    51 if(palindromes>right)
    52 return;
    53 printf("%d\n",palindromes);
    54 }
    55 if(cur_len<max_len)
    56 do_2();
    57 }
    58 void do_2()
    59 {
    60 int cur_len=2;
    61 int num1;
    62 int palindromes;
    63 for(num1=1;num1<=9;num1+=2){
    64 palindromes=11*num1;
    65 if(palindromes<left||!(prime(palindromes)))
    66 continue;
    67 if(palindromes>right)
    68 return;
    69 printf("%d\n",palindromes);
    70 }
    71 if(cur_len<max_len)
    72 do_3();
    73 }
    74 void do_3()
    75 {
    76 int cur_len=3;
    77 int num1,num2;
    78 int palindromes;
    79 for(num1=1;num1<=9;num1+=2){
    80 for(num2=0;num2<=9;num2++){
    81 palindromes=101*num1+10*num2;
    82 if(palindromes<left||!(prime(palindromes)))
    83 continue;
    84 if(palindromes>right)
    85 return;
    86 printf("%d\n",palindromes);
    87 }
    88 }
    89 if(cur_len<max_len)
    90 do_4();
    91 }
    92 void do_4()
    93 {
    94 int cur_len=4;
    95 int num1,num2;
    96 int palindromes;
    97 for(num1=1;num1<=9;num1+=2){
    98 for(num2=0;num2<=9;num2++){
    99 palindromes=num1*1001*110*num2;
    100 if(palindromes<left||!(prime(palindromes)))
    101 continue;
    102 if(palindromes>right)
    103 return;
    104 printf("%d\n",palindromes);
    105 }
    106 }
    107 if(cur_len<max_len)
    108 do_5();
    109 }
    110 void do_5()
    111 {
    112 int cur_len=5;
    113 int num1,num2,num3;
    114 int palindromes;
    115 for(num1=1;num1<=9;num1+=2){
    116 for(num2=0;num2<=9;num2++){
    117 for(num3=0;num3<=9;num3++){
    118 palindromes=10001*num1+1010*num2+num3*100;
    119 if(palindromes<left||!prime(palindromes))
    120 continue;
    121 if(palindromes>right)
    122 return;
    123 printf("%d\n",palindromes);
    124 }
    125 }
    126 }
    127 if(cur_len<max_len)
    128 do_6();
    129 }
    130 void do_6(){
    131 int cur_len=6;
    132 int num1,num2,num3;
    133 int palindromes;
    134 for(num1=1;num1<=9;num1++){
    135 for(num2=0;num2<=9;num2++){
    136 for(num3=0;num3<=9;num3++){
    137 palindromes=100001*num1+10010*num2+1100*num3;
    138 if(palindromes<left||!prime(palindromes))
    139 continue;
    140 if(palindromes>right)
    141 return;
    142 printf("%d\n",palindromes);
    143 }
    144 }
    145 }
    146 if(cur_len<max_len)
    147 do_7();
    148 }
    149 void do_7()
    150 {
    151 int cur_len=7;
    152 int num1,num2,num3,num4;
    153 int palindromes;
    154 for(num1=1;num1<=9;num1+=2){
    155 for(num2=0;num2<=9;num2++){
    156 for(num3=0;num3<=9;num3++){
    157 for(num4=0;num4<=9;num4++){
    158 palindromes=1000001*num1+100010*num2+10100*num3+1000*num4;
    159 if(palindromes<left||!prime(palindromes))
    160 continue;
    161 if(palindromes>right)
    162 return;
    163 printf("%d\n",palindromes);
    164 }
    165 }
    166 }
    167 }
    168 if(cur_len<max_len)
    169 do_8();
    170 }
    171
    172 void do_8()
    173 {
    174 int cur_len=8;
    175 int num1,num2,num3,num4;
    176 int palindromes;
    177 for(num1=1;num1<=9;num1+=2){
    178 for(num2=0;num2<=9;num2++){
    179 for(num3=0;num3<=9;num3++){
    180 for(num4=0;num4<=9;num4++){
    181 palindromes=10000001*num1+1000010*num2+100100*num3+11000*num4;
    182 if(palindromes<left||!prime(palindromes))
    183 continue;
    184 if(palindromes>right)
    185 return;
    186 printf("%d\n",palindromes);
    187 }
    188 }
    189 }
    190 }
    191 }
    192
    193
    194
    195 int main()
    196 {
    197 freopen("pprime.in","r",stdin);
    198 freopen("pprime.out","w",stdout);
    199 scanf("%d%d",&left,&right);
    200 get_len();
    201 switch(min_len){
    202 case 1:
    203 do_1();
    204 break;
    205 case 2:
    206 do_2();
    207 break;
    208 case 3:
    209 do_3();
    210 break;
    211 case 4:
    212 do_4();
    213 break;
    214 case 5:
    215 do_5();
    216 break;
    217 case 6:
    218 do_6();
    219 break;
    220 case 7:
    221 do_7();
    222 break;
    223 case 8:
    224 do_8();
    225 break;
    226 }
    227 }



  • 相关阅读:
    Javac配置环境变量01
    jmeter压测学习-发soap请求测试webservice
    Faker 安装与介绍
    docker使用nginx部署spring项目
    Linux查看端口占用情况
    Cesium 中两种添加 model 方法的区别
    Cesium 动态更换模型贴图方法
    Node.js API 学习笔记
    一步步建立 Vue + Cesium 初始化项目
    python3 解析 base64 数据
  • 原文地址:https://www.cnblogs.com/yangce/p/2344392.html
Copyright © 2011-2022 走看看