• IEEEXtreme 9.0


    博客中的文章均为 meelo 原创,请务必以链接形式注明 本文地址

    Xtreme 9.0 - Digit Fun!

    题目来源:第9届IEEE极限编程大赛第1题

    Recurrence relations are an important tool for the computer scientist. Many algorithms, particularly those that use divide and conquer, have time complexities best modeled by recurrence relations. A recurrence relation allows us to recursively define a sequence of values by defining the nth value in terms of certain of its predecessors.

    Many natural functions, such as factorials and the Fibonacci sequence, can easily be expressed as recurrences. The function of interest for this problem is described below.

    Let |An| denote the number of digits in the decimal representation of An. Given any number A0, we define a sequence using the following recurrence:

    Ai = |Ai-1| for i > 0

    The goal of this problem is to determine the smallest positive i such that Ai = Ai-1.

    Input Format

    Input consists of multiple lines, each terminated by an end-of-line character. Each line (except the last) contains a value for A0, where each value is non-negative and no more than a million digits. The last line of input contains the word END.

    Output Format

    For each value of A0 given in the input, the program should output one line containing the smallest positive i such that Ai = Ai-1.

    Sample Input

    9999
    0
    1
    9999999999
    END
    

    Sample Output

    3
    2
    1
    4
    

    Explanation

    The first input value is A0 = 9999, resulting in A1 = |9999| = 4. Because 4 does not equal 9999, we find A2 = |A1| = |4| = 1. Since 1 is not equal to 4, we find A3 = |A2| = |1| = 1. A3 is equal to A2, making 3 the smallest positive i such thatAi = Ai-1.

    The second input value is A0 = 0, resulting in A1 = |0| = 1. Because 0 does not equal 1, we find A2 = |A1| = |1| = 1. A2is equal to A1, making 2 the smallest positive i such that Ai = Ai-1.

    The third input value is A0 = 1, resulting in A1 = |1| = 1. A1 is equal to A0, making 1 the smallest positive i such thatAi = Ai-1.

    The last input value is A0 = 9999999999, resulting in A1 = |9999999999| = 10. Because 10 does not equal 9999999999, we find A2 = |A1| = |10| = 2. Since 2 is not equal to 10, we find A3 = |A2| = |2| = 1. Since 1 is not equal to 2, we find A4 = |A3| = |1| = 1. A4 is equal to A3, making 4 the smallest positive i such that Ai = Ai-1.

    Editorial

    The following editorial explains an approach for solving this problem.

    Given the potential size of the numbers, it is much easier to solve this problem if you attempt to store the values, not in integer variables, but rather as strings.

     
    程序 
    Python3
    while True:
        s = input()
        i = 1
        if s == 'END':
            break
        while s != str(len(s)):
            i += 1
            s = str(len(s))
        print(i)
  • 相关阅读:
    细心也是一种态度
    EDM数据访问的三种方式
    如何快速提交网站备案 ICP备案
    c# winform 关于给静态全局变量赋值的问题
    c#DIY随机数类winform 2010
    手把手教你如何用IIS搭建手机WAP网站(图文)
    admin密码对应的MD5值,16位和32位,admin解密自己留着方便.
    C#实现MD5加密,winform c#2005
    最全的c#日期函数 winform
    如何解决因网站备案号没下来而网站被迫关闭的办法
  • 原文地址:https://www.cnblogs.com/meelo/p/5931395.html
Copyright © 2020-2023  润新知