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. [转载]DOMContentLoaded与interactive

    http://www.cnblogs.com/muxrwc/archive/2011/01/13/1934379.html ie中inline script执行时竟然第一次进入页面,doc.ready ...

  2. ASP.NET乱码深度剖析

    写在前面 在Web开发中,乱码应该算一个常客了.今天还好好的一个页面,第二天过来打开一看,中文字符全变“外星文”了.有时为了解决这样的问题,需要花上很长的时间去调试,直至抓狂,笔者也曾经历过这样的时期 ...

  3. JS种this的四种用法

    记住以下四点: 1.没调用对象就指向全局对象 2.有对象就指向调用对象 3.用new构造就指向新对象 4.通过 apply 或 call 或 bind 来改变 this 的所指. 1.测试一:没调用对 ...

  4. Maven部署dao工程到私服上——(十三)

    1.修改settings.xml 需要在客户端即(部署dao工程)的电脑上配置 maven环境,并修改 settings.xml 文件,配置连接私服的用户和密码 . 此用户名和密码用于私服校验,因为私 ...

  5. 关于内核中spinlock的一些个人理解 【转】

    由于2.6内核可以抢占,应该在驱动程序中使用 preempt_disable() 和 preempt_enable(),从而保护代码段不被抢占(禁止 IRQ 同时也就隐式地禁止了抢占).preempt ...

  6. 07 Go 1.7 Release Notes

    Go 1.7 Release Notes Introduction to Go 1.7 Changes to the language Ports Known Issues Tools Assembl ...

  7. Windows安装pycrypto失败记录

    Windows 10家庭中文版,Python 3.6.4, 180824测试前端加密文本在后台揭秘,查询后发现,可以使用pycrypto模块实现,那么,安装它(pip),结果安装失败了. 本文暂时记录 ...

  8. c++中的类(class)

    c++的class(类)使用方法 这几天一直在调splay之类的东西,突然想转指针...qwq 于是,我就在沙华大佬的帮助下,学了下一顿乱指( $ -> $ ),也就是class(类) 首先:c ...

  9. sed的一些使用技巧

    一.当一个文件里有两行相同的内容,但这时只想修改第一行的内容或者第二行的内容,而不是全部修改,以下例子说明下: 1.修改匹配到第一行为port的内容(若要真修改前面记得-i): [root suppo ...

  10. hdu 1856 求集合里元素的个数 输出最大的个数是多少

    求集合里元素的个数 输出最大的个数是多少 Sample Input41 23 45 61 641 23 45 67 8 Sample Output42 # include <iostream&g ...