A1024. Palindromic Number (25)

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
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <iostream>
  4 #include <string.h>
  5 #include <string>
  6 #include <math.h>
  7 #include <algorithm>
  8 using namespace std;
  9 
 10 struct bign
 11 {
 12     int d[1010];
 13     int len;
 14     bign(){
 15         memset(d,0,sizeof(d));
 16         len=0;
 17     }
 18 };
 19 
 20 bign change(char str[]){
 21     bign a;
 22     a.len=strlen(str);
 23     for(int i=0;i<a.len;i++)
 24     {
 25         a.d[i]=str[a.len-i-1]-'0';
 26     }
 27     return a;
 28 }
 29 
 30 bign divide(bign a,int b,int & r)
 31 {
 32     bign c;
 33     c.len=a.len;
 34     for(int i=a.len-1;i>=0;i--)
 35     {
 36          r=10*r+a.d[i];
 37          if(r<b)c.d[i]=0;
 38          else
 39          {
 40           c.d[i]=r/b;
 41           r=r%b;    
 42          }    
 43     }
 44     while(c.len-1>0&&c.d[c.len-1]==0)
 45     {
 46         c.len--;
 47     }
 48     return c;
 49 }
 50 bign add(bign a,bign b)
 51 {
 52     bign c;
 53     int carry=0;
 54     for(int i=0;i<a.len||i<b.len;i++)
 55     {
 56         int temp=a.d[i]+b.d[i]+carry;
 57         c.d[c.len++]=temp%10;
 58         carry=temp/10;
 59     }
 60     if(carry!=0)
 61     {
 62     c.d[c.len++]=carry;    
 63     }
 64     return c;
 65 }
 66 
 67 bign multi(bign a,int b)
 68 {
 69     bign c;
 70     int carry=0;
 71     for(int i=0;i<a.len;i++)
 72     {
 73         int temp;
 74         temp=a.d[i]*b+carry;
 75         carry=temp/10;
 76         c.d[c.len++]=temp%10;
 77     }
 78     //乘法高位处理
 79     while(carry!=0)
 80     {
 81         c.d[c.len++]=carry%10;
 82         carry/=10; 
 83     }
 84     return c;
 85 }
 86 
 87 void print(bign a)
 88 {
 89   for(int i=a.len-1;i>=0;i--)
 90   {
 91       printf("%d",a.d[i]);
 92   }    
 93 }
 94 
 95 bool judge(bign a )
 96 {
 97     for(int i=0;i<a.len;i++)
 98     {
 99         if(a.d[i]!=a.d[a.len-1-i])
100         return false;
101     } 
102     return true;
103 }
104 int main(){
105     char str[1000];
106     int n;
107     scanf("%s %d",str,&n);
108     bign a=change(str);
109     int k=0;
110     while(judge(a)==false&&k<n)
111     {
112         bign b=a;
113         reverse(b.d,b.d+b.len);//数组右边界的下一位 
114         a=add(a,b);
115         k++;
116     }
117     print(a);
118     printf("
");
119     printf("%d",k);
120     return 0;
121 }
原文地址:https://www.cnblogs.com/ligen/p/4304485.html