Xtreme9.0 - Digit Fun!

题目连接:

https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/digit-fun

Description

An editorial, providing an approach to solve this problem, is presented at the bottom of this page.

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

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

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

Hint

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 that Ai = 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. A2 is 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 that Ai = 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.

题意

输入A[0]

定义F(x)=strlen(x)

A[i]=F(A[i-1])

求最小的i,使得A[i]=A[i-1]

题解

暴力水题

详细看代码

代码

#include<bits/stdc++.h>
using namespace std;
int last;
string s;
string get(string ss)
{
string tmp;
int p=ss.size();
while(p)
{
tmp+=p%10+'0';
p/=10;
}
reverse(tmp.begin(),tmp.end());
return tmp;
}
int main()
{
while(cin>>s)
{
if(s[0]=='E')break;//读到EOF时,break
last=0;
for(int i=0;i<s.size();i++)//将字符串转化为数字
last*=10,last+=s[i]-'0'; int now=1;
while(last!=s.size()){//暴力模拟过程
now++;
last=s.size();
s=get(s);//将处理过程
}
cout<<now<<endl;
}
}

IEEEXtreme Practice Community Xtreme9.0 - Digit Fun!的更多相关文章

  1. IEEEXtreme Practice Community Xtreme9.0 - Dictionary Strings

    Dictionary Strings 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/dictio ...

  2. IEEEXtreme 9.0 - Digit Fun!

    博客中的文章均为 meelo 原创,请务必以链接形式注明 本文地址 Xtreme 9.0 - Digit Fun! 题目来源:第9届IEEE极限编程大赛第1题 Recurrence relations ...

  3. Xtreme9.0 - Communities 强连通

    Xtreme9.0 - Communities 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/c ...

  4. Xtreme9.0 - Light Gremlins 容斥

    Xtreme9.0 - Light Gremlins 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenge ...

  5. Xtreme9.0 - Block Art 线段树

    Block Art 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/block-art Descr ...

  6. Xtreme9.0 - Taco Stand 数学

    Taco Stand 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/taco-stand Des ...

  7. Xtreme9.0 - Pattern 3 KMP

    Pattern 3 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/car-spark Descr ...

  8. Xtreme9.0 - Car Spark 动态规划

    Car Spark 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/car-spark Descr ...

  9. Xtreme9.0 - Mr. Pippo's Pizza 数学

    Mr. Pippo's Pizza 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/mr-pipp ...

随机推荐

  1. bzoj千题计划293:bzoj3142: [Hnoi2013]数列

    http://www.lydsy.com/JudgeOnline/problem.php?id=3142 如果已知数列的差分数列a[1]~a[k-1] 那么这种差分方式对答案的贡献为 N-Σ a[i] ...

  2. bzoj千题计划259:bzoj3122: [Sdoi2013]随机数生成器

    http://www.lydsy.com/JudgeOnline/problem.php?id=3122 等比数列求和公式+BSGS #include<map> #include<c ...

  3. Spring RedisTemplate操作-发布订阅操作(8)

    @Component("sub") public class Sub implements MessageListener{ @Autowired private StringRe ...

  4. javascript的单例/单体模式(Singleton)

    首先,单例模式是对象的创建模式之一,此外还包括工厂模式.单例模式的三个特点:1,该类只有一个实例2,该类自行创建该实例(在该类内部创建自身的实例对象)3,向整个系统公开这个实例接口 Java中大概是这 ...

  5. # 20155337 2016-2017-2 《Java程序设计》第六周学习总结

    20155337 2016-2017-2 <Java程序设计>第六周学习总结 教材学习内容总结 •串流(Stream): 数据有来源及目的地,衔接两者的是串流对象.如果要将数据从来源取出, ...

  6. 如何用Procmon.exe来监视SQLSERVER的logwrite大小

    如何用Procmon.exe来监视SQLSERVER的logwrite大小 在微软亚太区数据库技术支持组官方博客里面,你会发现很多篇文章都用到了Procmon.exe这个工具 今天我也介绍一下这个工具 ...

  7. 使用Cobbler批量部署Linux和Windows:Cobbler服务端部署(一)

    本文记录了我使用Cobbler批量安装部署Linux和Windows系统的过程,文章主要分为三部分:Cobbler服务端的安装配置.Linux发行版CentOS和Ubuntu的自动安装部署.Windo ...

  8. IE下常见兼容性问题总结

    概述 本小菜平时主要写后台程序,偶尔也会去写点前端页面,写html.css.js的时候,会同时开着ie6.ie7.ie8.ie9.chrome.firefox等浏览器进行页面测试,和大部分前端开发一样 ...

  9. Python 优雅获取本机 IP 方法

    原文 见过很多获取服务器本地IP的代码,个人觉得都不是很好,例如以下这些 不推荐:靠猜测去获取本地IP方法 #!/usr/bin/env python # -*- coding: utf-8 -*- ...

  10. eclipse 关联 Maven本地仓库的配置

    一.首先下载maven插件并配置maven的环境变量,可参考:http://jingyan.baidu.com/article/cb5d61050b8ee7005d2fe04e.html 二.打开ec ...