博客中的文章均为 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)

IEEEXtreme 9.0 - Digit Fun!的更多相关文章

  1. IEEEXtreme Practice Community Xtreme9.0 - Digit Fun!

    Xtreme9.0 - Digit Fun! 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/di ...

  2. IEEEXtreme 10.0 - Ellipse Art

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Ellipse Art 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank ...

  3. IEEEXtreme 10.0 - Full Adder

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Full Adder 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank. ...

  4. IEEEXtreme 10.0 - Inti Sets

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Inti Sets 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...

  5. IEEEXtreme 10.0 - Painter's Dilemma

    这是 meelo 原创的 IEEEXtreme极限编程比赛题解 Xtreme 10.0 - Painter's Dilemma 题目来源 第10届IEEE极限编程大赛 https://www.hack ...

  6. IEEEXtreme 10.0 - Counting Molecules

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Counting Molecules 题目来源 第10届IEEE极限编程大赛 https://www.hac ...

  7. IEEEXtreme 10.0 - Checkers Challenge

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Checkers Challenge 题目来源 第10届IEEE极限编程大赛 https://www.hac ...

  8. IEEEXtreme 10.0 - Game of Stones

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme 10.0 - Game of Stones 题目来源 第10届IEEE极限编程大赛 https://www.hackerr ...

  9. IEEEXtreme 10.0 - Food Truck

    这是 meelo 原创的 IEEEXtreme极限编程大赛题解 Xtreme10.0 - Food Truck 题目来源 第10届IEEE极限编程大赛 https://www.hackerrank.c ...

随机推荐

  1. ss命令用法小记

    By francis_hao    Nov 4,2017   ss是一个查看socket的实用工具 概要 ss [options] [ FILTER ]   描述 ss可以查看socket的统计信息, ...

  2. qq快速登陆

    http://www.cnblogs.com/1996V/p/7481823.html qq快速登陆

  3. 微信小程序踩过的坑

    之前用小程序开发工具做过一个项目了,最近又新开了一个项目,在登录的时候发现总是提示code不合法,找了半天也未发现原因 后来同事提醒是不是因为开发工具里设置的AppId的问题,果断将当前工具里默认Ap ...

  4. C/C++预处理宏的总结

    1.定义顺序的无关性 #define PI 3.14 #define TWO_PI  2*PI 这两句谁前谁后无所谓,因为预处理器不断迭代来实现宏替换,直到源文件中没有宏了才停止. 2. 宏变量变成字 ...

  5. Excel公式

    多个IF =IF(ISNUMBER(SEARCH("lz",E7)),"lz", IF(ISNUMBER(SEARCH("zch",E7)) ...

  6. web启动@Autowired不能自动注入

    使用struts2,下面为action代码 Java代码 package com.edar.web.platform; import org.apache.struts2.convention.ann ...

  7. 如何编写高质量的 jQuery 代码?

    想必大家对于jQuery这个最流行的javascript类库都不陌生,而且只要是前端开发人员肯定或多或少的使用或者接触过,在今天的这篇文章中,我们将介绍一些书写高质量jQuery代码的原则,我们不单单 ...

  8. CodeForces 990C

    Description A bracket sequence is a string containing only characters "(" and ")" ...

  9. 【洛谷 P3705】 [SDOI2017]新生舞会(费用流,01分数规划)

    题目链接 看到这题我想到了以前做过的一题,名字记不清了,反正里面有"矩阵"二字,然后是道二分图匹配的题. 经典的行列连边网络流. 第\(i\)行和第\(j\)列连边,费用为\(b[ ...

  10. linux学习记录.2.hello world.c

    安装vim,指令: sudo apt-get install vim 建立一个子目录WorkSpace,指令 mkdir WorkSpace 转到该目录下,指令 cd WorkSpace 新建c文件, ...