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. Python Thread

    lock 对象: acquire():负责取得一个锁.如果没有线程正持有锁,acquire方法会立刻得到锁.否则,它闲意态等锁被释放. 一旦acquire()返回,调用它的线程就持有锁. releas ...

  2. Objective-C基础笔记一

    这里开始了我OC旅程 花了8天的时间粗略的学习了新知识Objective-C(简称OC),虽然只是学习了其中的基础部分,但经过这一周的学习也算是入门了.对面向对象的封装.继承.多态以及其中所包含的方法 ...

  3. sql语句判断方法之一

    sql语句判断方法之一CASE语句用法总结 背景: Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex WHEN '1' THEN '男' WHEN ' ...

  4. 【TOMCAT】Tomcat gzip压缩传输数据

    概述 由于我们项目的三维模型文件非常大,为了提高传输速度,在服务端对其做zip压缩处理非常有必要,能够极大的提高传输速度. 配置 首先需要修改web.xml中请求的数据文件的mime类型的mappin ...

  5. spark RDD的元素顺序(ordering)测试

    通过实验发现: foreach()遍历的顺序是乱的 但: collect()取到的结果是依照原顺序的 take()取到的结果是依照原顺序的 为什么呢???? 另外,可以发现: take()取到了指定数 ...

  6. asp.net post方法;对象转json

    [System.Web.Services.WebMethod()]     public static string GetPoints(string userId)     {         st ...

  7. Ubuntu 14.0操作系统,修改默认打开方式的方法

    Ubuntu 14.0 有内置的视频播放器 Totem,但是使用起来不太习惯,所以在系统的软件中心 下载了gnome Mplayer和s Mplayer,都有打开上次播放的忆功能,只是gnome Mp ...

  8. 自定义实现简单的Android颜色选择器(附带源码)

    在写Android App过程中需要一个简单的颜色选择器,Android自带的ColorPicker和网上的一些ColorPicker都太高端了,都实现了颜色渐变功能,我要的不需要那么复杂,只想提供几 ...

  9. Struts升级到2.3.15.1抵抗漏洞

    后知后觉,今天才开始修复Struts2的漏洞 详细情形可以参考: http://struts.apache.org/release/2.3.x/docs/security-bulletins.html ...

  10. 【leetcode】7. Reverse Integer

    题目描述: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 解题思 ...