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;
    }
  • 相关阅读:
    mac上的键盘生活——打字训练

    011-黑盒测试的测试用例常见设计方法都有哪些?请分别以具体的例子来说明这些方法在测试用例设计工作中的应用
    010-如何测试一个 纸杯?
    009-条软件缺陷(或者叫 Bug)记录都包含了哪些内容?如何提交高质量的软件缺陷(Bug)记录?
    008-黑盒测试和白盒测试的优缺点
    007-测试人员在软件开发过程中的任务是什么?
    006- 软件产品质量特性是什么?
    005-目前主要的测试用例设计方法是什么?
    007-软件测试分类
  • 原文地址:https://www.cnblogs.com/TheLaughingMan/p/2913513.html
Copyright © 2011-2022 走看看