若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

1527: Reverse and Add

发表于 2017-10-06   |   分类于 HUSTOJ   |   阅读次数 1,269

题目描述

The reverse and add function starts with a number, reverses its digits and adds the reverse to the original. If the sum is not a palindrome (meaning it does not give the same number read from left to right and right to left), we repeat this procedure until it does.

For example, if we start with 195 as the initial number, we get 9,339 as the resulting palindrome after the fourth addition:

195 786 1,473 5,214
591 687 3,741 4,125
+ — + — + — + —
786 1,473 5,214 9,339

This method leads to palindromes in a few steps for almost all of the integers. But there are interesting exceptions. 196 is the first number for which no palindrome has been found. It has never been proven, however, that no such palindrome exists.

You must write a program that takes a given number and gives the resulting palindrome (if one exists) and the number of iterations/additions it took to find it.

You may assume that all the numbers used as test data will terminate in an answer with less than 1,000 iterations (additions), and yield a palindrome that is not greater than 4,294,967,295.

输入

The first line will contain an integer N ( 0 < N100), giving the number of test cases, while the next N lines each contain a single integer P whose palindrome you are to compute.

输出

For each of the N integers, print a line giving the minimum number of iterations to find the palindrome, a single space, and then the resulting palindrome itself.

样例输入

3
195
265
750


样例输出

4 9339
5 45254
3 6666


参考代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int hui( char b[] );
void add(char a[],char b[],char back[]);
int main() 
{
    int i, j;
    char a[100], b[100],c[100];
    int n, len;
    int cnt;
    scanf("%d", &n);
    getchar();
    for ( i = 0; i < n; i++ ) 
    {
        cnt = 1;
        memset(a, 0, sizeof(a));
        memset(c, 0, sizeof(c));
        memset(b,0, sizeof(b));
        gets(a);
        len = strlen(a);
        for ( j = len-1; j >= 0; j-- ) 
        {
            c[j] = a[len-1-j];
        }
        while(1) 
        {
            add(a, c, b);
            if( hui(b) == 0 ) 
            {
                break;
            }
            memset(a, 0, sizeof(a));
            memset(c, 0, sizeof(c));
            strcpy(a, b);
            len = strlen(b);
            for ( j = len-1; j >= 0; j-- ) 
            {
                c[j] = a[len-1-j];
            }
            cnt++;
            memset(b,0, sizeof(b));
        }
        printf("%d ", cnt);
        puts(b);
    }
    return 0;
}
void add(char a[],char b[],char back[]) 
{
    int i,j,k,up,x,y,z,l;
    char *c;
    if (strlen(a)>strlen(b)) l=strlen(a)+2; else l=strlen(b)+2;
    c=(char *) malloc(l*sizeof(char));
    i=strlen(a)-1;
    j=strlen(b)-1;
    k=0;
    up=0;
    while(i>=0||j>=0) 
    {
        if(i<0) x='0'; else x=a[i];
        if(j<0) y='0'; else y=b[j];
        z=x-'0'+y-'0';
        if(up) z+=1;
        if(z>9) 
        {
            up=1;
            z%=10;
        } else up=0;
        c[k++]=z+'0';
        i--;
        j--;
    }
    if(up) c[k++]='1';
    i=0;
    c[k]='';
    for(k-=1;k>=0;k--)
        back[i++]=c[k];
    back[i]='';
} 
int hui( char a[] )
{
    int i;
    int flag = 0;
    int len;
    len = strlen(a);
    for( i = 0; i < len/2; i++ )
    {
        if( a[i] != a[len-1-i] )
        {
            flag = 1;
        }
    }
    if( flag )
    {
        return 1;
    }
    return 0;
}

解析

暂无

hustoj

发表评论 取消回复

邮箱地址不会被公开。 必填项已用*标注

*
*


hoxis wechat
著作权归作者所有
站点更新说明
  • 文章目录
  • 站点概览
若是凉夜已成梦

若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。

1904 日志
6 分类
12 标签
RSS
weibo github twitter facebook

友情链接

Dreams孤独患者 Skip 原站点
© 2017 若是凉夜已成梦
Powered by WordPress | 已运行
Theme By NexT.Mist