zoukankan      html  css  js  c++  java
  • [解题报告]10018 Reverse and Add

    Reverse and Add

    The Problem

    The "reverse and add" method is simple: choose a number, reverse its digits and add it to the original. If the sum is not a palindrome (which means, it is not the same number from left to right and right to left), repeat this procedure.

    For example:
    195 Initial number
    591
    -----
    786
    687
    -----
    1473
    3741
    -----
    5214
    4125
    -----
    9339 Resulting palindrome

    In this particular case the palindrome 9339 appeared after the 4th addition. This method leads to palindromes in a few step for almost all of the integers. But there are interesting exceptions. 196 is the first number for which no palindrome has been found. It is not proven though, that there is no such a palindrome.

    Task :
    You must write a program that give the resulting palindrome and the number of iterations (additions) to compute the palindrome.

    You might assume that all tests data on this problem:
    - will have an answer ,
    - will be computable with less than 1000 iterations (additions),
    - will yield a palindrome that is not greater than 4,294,967,295.
     

    The Input

    The first line will have a number N with the number of test cases, the next N lines will have a number P to compute its palindrome.

    The Output

    For each of the N tests you will have to write a line with the following data : minimum number of iterations (additions) to get to the palindrome and the resulting palindrome itself separated by one space.

    Sample Input

    3
    195
    265
    750
    

    Sample Output

    4 9339
    5 45254
    3 6666

    题目顺着难易度表做到这里就有点开始考脑力了,题目中一个比较关键的地方有:

    一、如何把输入的数处理成回文数

    看了几个网上的AC代码,大部分的思路是储存成字符串,然后倒叙存储

    那么,有没有用UNSIGNED型呢?毕竟题目中提到了“- will yield a palindrome that is not greater than 4,294,967,295.”

    我想了一种办法,可以处理出回文数

    1、把原数temp=p除十取余,得到原数个位数,加到到P_reverse(初值为0)

    2、再将P_reverse乘十,原数除十

    3、反复1,2直到原数为0

    unsigned int temp=P,P_reverse=0;
                while(temp)
                {
                    P_reverse*=10;
                    P_reverse+=temp%10;
                    temp/=10;
                }
                

    这样就得到了了回文数P_reverse

    AC代码

    #include<stdio.h>
     
    int main()
    {
        int N;
        while(scanf("%d",&N)!=EOF)
        {
            int i;
            for(i=1;i<=N;i++)
            {
                unsigned int P;
                scanf("%u",&P );
                unsigned int temp=P,P_reverse=0;
                while(temp)
                {
                    P_reverse*=10;
                    P_reverse+=temp%10;
                    temp/=10;
                }
                int add = 0;
                do
                {
                    P+= P_reverse;
                    add++;
                    temp=P;
                    P_reverse=0;
                    while(temp)
                    {
                        P_reverse*=10;
                        P_reverse+=temp%10;
                        temp/=10;
                    }
                }while(P!=P_reverse);
                printf("%d %u\n",add,P);
            }
        }
        return 0;
    }
  • 相关阅读:
    4、配置解决中文乱码的过滤器
    3、请求参数绑定
    2、SpringMVC常用注解
    SpringMVC快速搭建
    深拷贝与浅拷贝笔记
    SpringBoot入门(三)——SpringData JPA
    SpringBoot入门(二)——Web
    SpringBoot入门(一)——HelloWorld、配置、日志
    Java SSM(十八)——Mybatis查缺补漏
    Java SSM(十七)——SpringMVC查缺补漏
  • 原文地址:https://www.cnblogs.com/TheLaughingMan/p/2913513.html
Copyright © 2011-2022 走看看