博客中的文章均为 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. 使用Hexo搭建GitHub博客(2018年Mac版)

    关于本文 本文仅记录自己学习搭建Hexo博客之时,搭建过程中掉坑的历程总结,对零基础起步的观众朋友可能缺乏某些基础技术的指导,请优先食用下述两篇优质教程: [2018更新]小白独立搭建博客-Githu ...

  2. tomcat8 的 websocket 支持

    使用 tomcat8 开发 WebSocket 服务端非常简单,大致有如下两种方式. 1.使用注解方式开发,被 @ServerEndpoint 修饰的 Java 类即可作为 WebSocket 服务端 ...

  3. 我是大SB

    哈哈哈 我就是个大SB!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  4. OpenCV---图像金字塔原理

    图像金字塔原理 (一)图像缩小(先高斯模糊,再降采样,需要一次次重复,不能一次到底) (二)图像扩大(先扩大,再卷积或者使用拉普拉斯金字塔) 图像金字塔介绍 图像金字塔是图像中多尺度表达的一种,最主要 ...

  5. Vue.js随笔三(npm init webpack my-project指令安装失败解决方案)

    如果没有安装淘宝给的镜像就先安装一下,指令如下,对!就是如此简单: npm install -g cnpm -registry=https://registry.npm.taobao.org 首先输入 ...

  6. Mongodb 备份 还原 导出 导入 等批量操作

    mongodb数据备份和还原主要分为二种,一种是针对于库的mongodump和mongorestore,一种是针对库中表的mongoexport和mongoimport. 一,mongodump备份数 ...

  7. MySql 插入数据库报错 Incorrect string value: '\xF0\xA0\x86\xA2'

    今天从nginx日志分析搜索关键字,然后把关键字插入到Mysql数据库里,出现如下错误 SQL state [HY000]; error code [1366]; Incorrect string v ...

  8. 2017-2018-2 20179207 《网络攻防技术》第十三周作业 python3实现SM234算法

    国密算法SM234 的python3实现 国家标准 GM/T 0002-2012 <SM4分组密码算法> GM/T 0003.1-2012 <SM2椭圆曲线公钥密码算法 第1部分:总 ...

  9. 2015/11/1用Python写游戏,pygame入门(1):pygame的安装

    这两天学习数据结构和算法,有时感觉并不如直接做项目来的有趣.刚刚学完python的基本使用,现在刚好趁热打铁做个小项目. 由于本人一直很想制作一款游戏,就想使用Python制作一个基础的游戏.搜了一下 ...

  10. Handlerbars基础笔记

    此笔记摘抄于杨元的博客(http://www.cnblogs.com/iyangyuan/archive/2013/12/12/3471227.html) 引入 <script type=&qu ...