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. SQL Server2008创建约束图解 转

    转自 http://www.cnblogs.com/longhs/p/3670307.html SQLServer 中有五种约束, Primary Key 约束. Foreign Key 约束. Un ...

  2. python(1) - 变量小测试

    下面看一道题目: a = 3 b = a a = 5 print(a,b) 程序最后输出的a,b值分别是多少呢? 先想一想,然后我们执行一下看看正确答案: 正确答案 a 的值是5, b的值是3,为什么 ...

  3. 深入理解计算机系统第二版习题解答CSAPP 2.20

    T2Uw(w)=x, x≥0时 T2Uw(w)=x+2w, x<0时 利用上面的公式,重新计算2.19的问题.

  4. 远程管理之VNC

    远程管理的基本概念 ①RDP(remote desktop protocol)协议 远程桌面协议,我们常用的windows操作系统就是的远程桌面管理就是基于该协议的,更多有关RDP协议的可以查看百度百 ...

  5. [改善Java代码]易变业务使用脚本语言编写

    建议16: 易变业务使用脚本语言编写 Java世界一直在遭受着异种语言的入侵,比如PHP.Ruby.Groovy.JavaScript等,这些“入侵者”都有一个共同特征:全是同一类语言—脚本语言,它们 ...

  6. 关于servlet与jsp&java类传值问题

    为了实现单击左侧导航栏,动态改变右侧(一个jsp文件)的内容,需要改变变量var的值,进而实现改变default部分内容的目的(自己想的方法,因为实在是layman.应该有简便快捷的方法,可我不知道. ...

  7. EasyUI的datagrid获取所有正在编辑状态的行的行编号

    今天项目需要用了下EasyUI的datagrid的行编辑功能,跟着API来,只要是将各种状态时的处理逻辑弄好,还是蛮不错的. 开发过程中,遇到了个问题,在编辑完成后我需要获取datagrid所有处于编 ...

  8. C#操作Excel数据增删改查示例

    Excel数据增删改查我们可以使用c#进行操作,首先创建ExcelDB.xlsx文件,并添加两张工作表,接下按照下面的操作步骤即可 C#操作Excel数据增删改查. 首先创建ExcelDB.xlsx文 ...

  9. Activity的启动模式(android:launchMode)

    在android里,有4种activity的启动模式,分别为: “standard” (默认) “singleTop” “singleTask” “singleInstance” 它们主要有如下不同: ...

  10. mvc Web api 如何在控制器中调用

    关于如何调用 mvc Web api 的方法,网上一搜就是一大把,基本都是在前台jq中调用的,但是如何在后台调用呢? 本楼主做了一下测试,仅供参考. 先写一个简单的api,如下:[域1] namesp ...