PAT (Advanced Level) Practice——1024 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 (<= 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.

二、题意理解

给出一个不大于10^10的数字N和一个最大变换次数K,如果该数字不是回文数,则把该数字变为(自身+自身反转后的数字),接着继续进行回文数的判断或数字变换,直到该数字变成回文数或者变换次数达到上限。

三、算法思想

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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值