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. cocos2d-x中使用Http

    一.如何使用 //发送接口 void CmdHelper::postRequest(const char* cmdTag, const char* url, const char* postData, ...

  2. uva 10152 ShellSort 龟壳排序(希尔排序?)

    今天状态总是很糟,这种题目卡了一天... 是不是休息时间太少了,头脑迟钝了... 名字叫希尔排序,我还以为跟它有关,还搜索了下资料. 只要找到trick就会发现是很水的题目.只要对比下就能找到哪些是移 ...

  3. Gnome下Gvim菜单无法显示的解决办法

    前些日子从Xfce转向了Elementary OS,安装Gvim后发现菜单无法显示,现在找到了解决的办法. 编辑文件  -/.gnome2/vim [Placement] Dock=Toolbar\\ ...

  4. Android NDK开发之Android.mk文件

    Android NDK开发指南---Android.mk文件 博客分类: Android NDK开发指南   Android.mk文件语法详述 介绍: ------------ 这篇文档是用来描述你的 ...

  5. 【转】创业C2C(Copy To China):停车位共享APP,用户、市政能够买账?

    如果周六中午想开车到旧金山的Mission吃顿早午餐,笔者劝您还是省省吧.因为不光是到了吃饭的地儿排队得耗上一个小时,就是满大街的兜圈子找停车位都能折腾死人.那个时候您或许就明白了,其实最苦的并不是买 ...

  6. 归约函数reduce&映射数组map(笔记)

    function forEach(array,action){ ;i<array.length;i++) action(array[i]); } function reduce(combine, ...

  7. Lombok(1.14.8) - @Log

    @Log @Logs,生成一个日志对象. package com.huey.lombok; import lombok.extern.java.Log; @Log public class LogEx ...

  8. double的值太大,以及补0

    当double的值太大的时候,比如1000000000 用DecimalFormat: double d = 1.0E7; System.out.println(new DecimalFormat(& ...

  9. ASP.NET网站前端页面的复制

    网络普及的时代,遇到问题的首要解决方案并不是问人,而是找度娘.当我们找一些技术性的问题时,会发现很多解决方案在博客里,看看博主发表的博客总是惊叹不已,想要自己也有这么一个好习惯,把学到的东西以自己的方 ...

  10. ASP.NET中页面加载时文本框(texbox控件)内有文字获得焦点时文字消失

    代码如下: <asp:TextBox ID="TextBox1" runat="server" Height="26px" MaxLe ...