zoukankan      html  css  js  c++  java
  • Palindromic Number (还是大数)

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Number. For example, 1234321 is a palindromic number. All single digit numbers are palindromic numbers.

    Non-palindromic numbers can be paired with palindromic ones via a series of operations. First, the non-palindromic number is reversed and the result is added to the original number. If the result is not a palindromic number, this is repeated until it gives a palindromic number. For example, if we start from 67, we can obtain a palindromic number in 2 steps: 67 + 76 = 143, and 143 + 341 = 484.

    Given any positive integer N, you are supposed to find its paired palindromic number and the number of steps taken to find it.

    Input Specification:

    Each input file contains one test case. Each case consists of two positive numbers N and K, where N (<= 1010) is the initial numer and K (<= 100) is the maximum number of steps. The numbers are separated by a space.

     Output Specification:

    For each test case, output two numbers, one in each line. The first number is the paired palindromic number of N, and the second number is the number of steps taken to find the palindromic number. If the palindromic number is not found after K steps, just output the number obtained at the Kth step and K instead.

    Sample Input 1:

    67 3

     Sample Output 1:

    484

    2

     Sample Input 2:

    69 3

     Sample Output 2:

    1353

    3

      1 #include <iostream>
      2 
      3 #include <string>
      4 
      5 #include <algorithm>
      6 
      7 using namespace std;
      8 
      9  
     10 
     11 int aa1[50];
     12 
     13 int aa2[50];
     14 
     15  
     16 
     17 int main()
     18 
     19 {
     20 
     21  
     22 
     23       string  n;int k;
     24 
     25     while(cin>>n)
     26 
     27       {
     28 
     29             cin>>k;
     30 
     31           int i,j,t;
     32 
     33  
     34 
     35        bool ifid=true;
     36 
     37  
     38 
     39          for(i=0,j=n.length()-1;i<=j;i++,j--)
     40 
     41          {
     42 
     43              if(n[i]!=n[j])
     44 
     45                {
     46 
     47                  ifid=false;
     48 
     49                   break;
     50 
     51                }
     52 
     53          }
     54 
     55  
     56 
     57          if(ifid)
     58 
     59          {
     60 
     61             cout<<n<<endl;
     62 
     63               cout<<0<<endl;
     64 
     65          }
     66 
     67          else
     68 
     69          {
     70 
     71                 for(i=0;i<50;i++)
     72 
     73                   {
     74 
     75                     aa1[i]=0;
     76 
     77                        aa2[i]=0;
     78 
     79                   }
     80 
     81                 int count=0;
     82 
     83                 for(i=n.length()-1;i>=0;i--)
     84 
     85                   {
     86 
     87                   aa1[count]=n[i]-'0';
     88 
     89                     aa2[count]=n[i]-'0';
     90 
     91                     count++;
     92 
     93                   }
     94 
     95  
     96 
     97                 reverse(aa2,aa2+count);
     98 
     99               int tem=0;
    100 
    101                   int sum=0;
    102 
    103                 for(i=1;i<=k;i++)
    104 
    105                   {
    106 
    107                      for(j=0;j<count;j++)
    108 
    109                               aa1[j]=aa1[j]+aa2[j];
    110 
    111                         sum++;
    112 
    113                  for(j=0;j<count;j++)
    114 
    115                                 {
    116 
    117                               if(aa1[j]>9)
    118 
    119                                       {
    120 
    121                                  tem=aa1[j]/10;
    122 
    123                                  aa1[j+1]=aa1[j+1]+tem;
    124 
    125                                  aa1[j]=aa1[j]%10; 
    126 
    127                                       }
    128 
    129                                 }
    130 
    131                         if(aa1[j]!=0) count++;
    132 
    133  
    134 
    135                         
    136 
    137                   bool ifis=true;
    138 
    139  
    140 
    141                     for(j=0,t=count-1;j<=t;j++,t--)
    142 
    143                           {
    144 
    145                          if(aa1[j]!=aa1[t])
    146 
    147                                  {
    148 
    149                              ifis=false;
    150 
    151                                break;
    152 
    153                                  }
    154 
    155                           }
    156 
    157  
    158 
    159                     if(ifis)
    160 
    161                           {
    162 
    163                       break;
    164 
    165                           }
    166 
    167                           else
    168 
    169                           {
    170 
    171                             for(j=0;j<count;j++)
    172 
    173                                     aa2[j]=aa1[j];
    174 
    175                               reverse(aa2,aa2+count);
    176 
    177                           }
    178 
    179                   }
    180 
    181  
    182 
    183  
    184 
    185                   for(j=count-1;j>=0;j--)
    186 
    187                         cout<<aa1[j];
    188 
    189                   cout<<endl;
    190 
    191                   cout<<sum<<endl;
    192 
    193          }
    194 
    195  
    196 
    197       }
    198 
    199       return 0;
    200 
    201 }
    View Code
  • 相关阅读:
    mysql 递归查询sql语句的实现
    Windows安装MySQL-5.6.13免安装版配置方法
    URL中的特殊字符处理
    Java序列化与反序列化
    url特殊字符转义及解决方法
    CentOs Mysql主从复制与读写分离
    java 转义特殊字符
    List列表中根据某一个字段,进行去重
    Spring定时任务的几种实现Quartz Timer task
    文件操作
  • 原文地址:https://www.cnblogs.com/xiaoyesoso/p/4240612.html
Copyright © 2011-2022 走看看