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

 #include <iostream>

 #include <string>

 #include <algorithm>

 using namespace std;

 int aa1[];

 int aa2[];

 int main()

 {

       string  n;int k;

     while(cin>>n)

       {

             cin>>k;

           int i,j,t;

        bool ifid=true;

          for(i=,j=n.length()-;i<=j;i++,j--)

          {

              if(n[i]!=n[j])

                {

                  ifid=false;

                   break;

                }

          }

          if(ifid)

          {

             cout<<n<<endl;

               cout<<<<endl;

          }

          else

          {

                 for(i=;i<;i++)

                   {

                     aa1[i]=;

                        aa2[i]=;

                   }

                 int count=;

                 for(i=n.length()-;i>=;i--)

                   {

                   aa1[count]=n[i]-'';

                     aa2[count]=n[i]-'';

                     count++;

                   }

                 reverse(aa2,aa2+count);

               int tem=;

                   int sum=;

                 for(i=;i<=k;i++)

                   {

                      for(j=;j<count;j++)

                               aa1[j]=aa1[j]+aa2[j];

                         sum++;

                  for(j=;j<count;j++)

                                 {

                               if(aa1[j]>)

                                       {

                                  tem=aa1[j]/;

                                  aa1[j+]=aa1[j+]+tem;

                                  aa1[j]=aa1[j]%; 

                                       }

                                 }

                         if(aa1[j]!=) count++;

                   bool ifis=true;

                     for(j=,t=count-;j<=t;j++,t--)

                           {

                          if(aa1[j]!=aa1[t])

                                  {

                              ifis=false;

                                break;

                                  }

                           }

                     if(ifis)

                           {

                       break;

                           }

                           else

                           {

                             for(j=;j<count;j++)

                                     aa2[j]=aa1[j];

                               reverse(aa2,aa2+count);

                           }

                   }

                   for(j=count-;j>=;j--)

                         cout<<aa1[j];

                   cout<<endl;

                   cout<<sum<<endl;

          }

       }

       return ;

 }

Palindromic Number (还是大数)的更多相关文章

  1. PAT甲题题解-1024. Palindromic Number (25)-大数运算

    大数据加法给一个数num和最大迭代数k每次num=num+num的倒序,判断此时的num是否是回文数字,是则输出此时的数字和迭代次数如果k次结束还没找到回文数字,输出此时的数字和k 如果num一开始是 ...

  2. PAT 甲级 1024 Palindromic Number (25 分)(大数加法,考虑这个数一开始是不是回文串)

    1024 Palindromic Number (25 分)   A number that will be the same when it is written forwards or backw ...

  3. PAT A1024 Palindromic Number (25 分)——回文,大整数

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  4. PAT 1024 Palindromic Number[难]

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  5. PTA (Advanced Level) 1024 Palindromic Number

    Palindromic Number A number that will be the same when it is written forwards or backwards is known ...

  6. 1024 Palindromic Number (25 分)

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  7. General Palindromic Number (进制)

    A number that will be the same when it is written forwards or backwards is known as a Palindromic Nu ...

  8. [ACM] ZOJ 3816 Generalized Palindromic Number (DFS,暴力枚举)

    Generalized Palindromic Number Time Limit: 2 Seconds      Memory Limit: 65536 KB A number that will ...

  9. PAT1019:General Palindromic Number

    1019. General Palindromic Number (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN ...

随机推荐

  1. Android防微信首页左右滑动切换

    大家看到微信首页切换效果有没有觉得很炫,滑动切换,点击底部bar瞬间切换,滑动切换渐变效果,线上效果图: 之前也在博客上看到别人的实现,再次基础上,我做了些优化.首先说下实现原理,大神略过,o(╯□╰ ...

  2. GIT免密码PUSH

    摘自:dudu 备忘,感谢! 1. 在Windows中添加一个HOME环境变量,值为%USERPROFILE%,如下图: 2. 在“开始>运行”中打开%Home%,新建一个名为“_netrc”的 ...

  3. [转]在SQL中用正则表达式替换html标签

    本文转自:http://blog.csdn.net/dhttaso/article/details/6045380 由于数据库的一个表字段中多包含html标签,现在需要修改数据库的字段把html标签都 ...

  4. HDU 3533 Escape (BFS + 预处理)

    Escape Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total S ...

  5. js学习笔记—转载(闭包问题)

    ---恢复内容开始--- 闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现.     一.变量的作用域 要理解闭包,首先必须理解Javascrip ...

  6. spring+hibernate管理多个数据源(非分布式事务)

    本文通过一个demo,介绍如何使用spring+hibernate管理多个数据源,注意,本文的事务管理并非之前博文介绍的分布式事务. 这个demo将使用两个事务管理器分别管理两个数据源.对于每一个独立 ...

  7. ubuntu下mysql的环境搭建及使用

    ubuntu下mysql的环境搭建及使用 环境安装 使用如下命令分别安装服务端程序,客户端程序,及客户端依赖库 sudo apt-get install mysql-server sudo apt-g ...

  8. LeftOuterJoin和OuterApply性能比较(转)

    建立测试环境: 建立一个表Department和Employee,并向Department插入50W条记录,向Employee插入200W条记录, 我们就拿[统计DepartmentID 从15000 ...

  9. OC3_歌词解析

    // // LrcManager.h // OC3_歌词解析 // // Created by zhangxueming on 15/6/15. // Copyright (c) 2015年 zhan ...

  10. OpenGL8-直接分配显存-极速绘制(Opengl1.5版本才有)

    视频教程请关注 http://edu.csdn.net/lecturer/lecturer_detail?lecturer_id=440 /** * 这个例子介绍如何使用显卡内存进行绘制 下载地址 : ...