一、题目
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 (<= 10^10^) 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.
二、题意理解
三、算法思想
1、考虑到数字的位数会不断增加且需要进行反转,这里采用双向链表存储数字的每一位(头尾两指针)。
2、回文数的判断:预先统计数字的位数,头尾两指针依次向中间移动并判断结点数字是否相等即可。
3、数字变换(自身+自身反转后的数字):将存储原数字的双向链表L1进行反转得到一个新双向链表L2,两个链表从尾部向头部(低位至高位)进行加法运算,运算结果存储于L1,最高位若出现进位则在L1头部新建一个结点。
注意:进行加法运算时,用掉一个L2的结点数字就释放掉相应结点,避免占用大量的内存空间。
四、C语言实现
#include <stdio.h>
#include <stdlib.h>
typedef struct node{//存储数位的结点
int digit;
struct node *prev,*next;
}Node,*Position;
Position front,rear;//存储数字的双向链表的头尾指针
int maxstep,length=0;//最大变换次数,数字位数
Position getNewNode(int n)
{
Position newnode=(Position)malloc(sizeof(Node));
newnode->digit=n;
newnode->next=newnode->prev=NULL;
return newnode;
}
void Read()//创建初始链表
{
char c;
Position p;
while((c=getchar())!=' '){
p=getNewNode(c-'0');
length++;
if(!front)
front=rear=p;
else{
rear->next=p;
p->prev=rear;
rear=p;
}
}
scanf("%d",&maxstep);
}
int IsPalindromic()//判断是否是回文数
{
Position p=front,q=rear;
int i;
for(i=1;i<=length/2;p=p->next,q=q->prev,++i){//头尾指针依次向中间移动
if(p->digit!=q->digit)
return 0;
}
return 1;
}
void PrintRes(Position p)//打印数字
{
while(p){
printf("%d",p->digit);
p=p->next;
}
printf("\n");
}
void Deal()//数字变换
{
Position p,q,qs,qe,temp;
p=front;
qs=qe=NULL;//存储反转后数字的双向链表的头尾指针
while(p){
Position temp=getNewNode(p->digit);
if(!qs)
qs=qe=temp;
else{
temp->next=qs;
qs->prev=temp;
qs=temp;
}
p=p->next;
}
int cur=0,sum;//进位
p=rear;//原始链表的尾部指针
q=qe;//新链表的尾部指针
while(p){
sum=p->digit+q->digit+cur;
p->digit=sum%10;
cur=sum/10;
p=p->prev;
temp=q;
q=q->prev;
free(temp);//及时释放内存
}
if(cur){//最高位有进位
temp=getNewNode(cur);
temp->next=front;
front->prev=temp;
front=temp;
length++;
}
}
int main()
{
front=rear=NULL;
Read();
int count=0;
for(;count<maxstep&&!IsPalindromic();count++)
Deal();
PrintRes(front);
printf("%d\n",count);
return 0;
}