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(java)学习笔记91:泛型接口的概述和使用

    package cn.itcast_06; /* * 泛型接口:把泛型定义在接口上 */ public interface Inter<T> { public abstract void ...

  2. oracle存储过程分页

    1.首先在oracle中建包体,用于游标返回当前数据记录集 CREATE OR REPLACE PACKAGE pkg_query AS TYPE cur_query IS REF CURSOR; E ...

  3. linux云计算集群架构学习笔记:系统文件的目录结构

    文件的基本管理和XFS文件系统备份恢复 1.1  Linux系统目录结构,相对/绝对路径. 1.2  创建/复制/删除文件,rm -rf / 意外事故 1.3 查看文件内容 1.4 xfs文件系统的备 ...

  4. Android编程心得-ListView的Item高亮显示的办法

    在我们使用ListView的时候,经常会遇到某一项(Item)需要高亮显示的情况,如下图,有人说当我们点击子项的时候会变亮,但有时候业务逻辑需要让ITEM根据条件自动变亮,下面我来介绍一下我自己的解决 ...

  5. 蓝牙 MultipeerConnectivity

    在iOS7中,引入了一个全新的框架——Multipeer Connectivity(多点连接). 利用Multipeer Connectivity框架,即使在没有连接到WiFi(WLAN)或移动网络( ...

  6. PHP中的Trait

    Trait 自 PHP 5.4.0 起,PHP 实现了一种代码复用的方法,称为 trait. Trait 是为类似 PHP 的单继承语言而准备的一种代码复用机制.Trait 为了减少单继承语言的限制, ...

  7. Commons JXPath - Object Graph Traversal

    JXPath 提供了使用 Xpath 语法操纵符合 Java 类命名规范的 JavaBeans 的工具.也支持 maps.DOM 和其他对象模型.对于深层次结构的 JavaBean,使用 JXPath ...

  8. HDOJ2011多项式求和

    多项式求和 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  9. Centos6.5 64linux系统基础优化(一)

    1  SecureCRT配色方案 http://blog.csdn.net/zklth/article/details/8937905 2  32位和64位Centos linux系统的区别及实际查看 ...

  10. (转)实战Memcached缓存系统(6)Memcached CAS的多线程程序实例

    1. 源程序 package com.sinosuperman.memcached; import java.io.IOException; import java.net.InetSocketAdd ...