IEEEXtreme Practice Community Xtreme9.0 - Digit Fun!
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!的更多相关文章
- IEEEXtreme Practice Community Xtreme9.0 - Dictionary Strings
Dictionary Strings 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/dictio ...
- IEEEXtreme 9.0 - Digit Fun!
博客中的文章均为 meelo 原创,请务必以链接形式注明 本文地址 Xtreme 9.0 - Digit Fun! 题目来源:第9届IEEE极限编程大赛第1题 Recurrence relations ...
- Xtreme9.0 - Communities 强连通
Xtreme9.0 - Communities 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/c ...
- Xtreme9.0 - Light Gremlins 容斥
Xtreme9.0 - Light Gremlins 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenge ...
- Xtreme9.0 - Block Art 线段树
Block Art 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/block-art Descr ...
- Xtreme9.0 - Taco Stand 数学
Taco Stand 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/taco-stand Des ...
- Xtreme9.0 - Pattern 3 KMP
Pattern 3 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/car-spark Descr ...
- Xtreme9.0 - Car Spark 动态规划
Car Spark 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/car-spark Descr ...
- Xtreme9.0 - Mr. Pippo's Pizza 数学
Mr. Pippo's Pizza 题目连接: https://www.hackerrank.com/contests/ieeextreme-challenges/challenges/mr-pipp ...
随机推荐
- bzoj千题计划207:bzoj1879: [Sdoi2009]Bill的挑战
http://www.lydsy.com/JudgeOnline/problem.php?id=1879 f[i][j] 表示匹配了i个字符,匹配字符串的状态为j的方案数 枚举下一个字符是什么 计算加 ...
- mysql 创建用户自定义函数
为了防止分号产生的中途输出,自己定义一个 分隔符,这里仿照mysql官方的例子:使用两个美元符号 $$ 作为分割符号,下面这段代码就是创建一个自定义mysql函数的原型了,可以在这个基础上修改,这样, ...
- Web性能优化系列(2):剖析页面绘制时间
本文由 伯乐在线 - J.c 翻译,sunbiaobiao 校稿.未经许可,禁止转载!英文出处:www.deanhume.com.欢迎加入翻译小组. 最近,我参加了在伦敦举办的Facebook移动开发 ...
- 第5月第24天 线性变换 opengl
1. http://news.qiyeku.com/news_837979.html 2. opengl + (Class)layerClass { return [CAEAGLLayer class ...
- if语句引起的bug
最近维护高手留下的api项目,客户端反馈一个bug过来,然后查找到可能出错的代码位置,是一个if语句,乍一看好像没什么问题,代码如下: if (company.UserId != userId || ...
- XShell 使用方法
XShell是一款Windows下非常优秀的远程连接Linux主机的工具,是平常使用不可缺少的工具.复制和粘贴由于在linux的Shell下,Ctrl+c是中断当前指令,这个快捷键和win系统下的复制 ...
- 从零开始自己搭建复杂网络(以Tensorflow为例)
从零开始自己搭建复杂网络(以MobileNetV2为例) tensorflow经过这几年的发展,已经成长为最大的神经网络框架.而mobileNetV2在经过Xception的实践与深度可分离卷积的应用 ...
- mybatis,genarate自动生成代码
---恢复内容开始--- generatorConfig.xml配置文件: <?xml version="1.0" encoding="UTF-8"?&g ...
- Mybatis输入映射和输出映射
本节内容: 输入参数映射 输出映射 resultMap Mapper.xml映射文件中定义了操作数据库的sql,每个sql是一个statement,映射文件是mybatis的核心. 一.环境准备 复制 ...
- Qwidget+opencv显示图像
步骤 1. 设置opencv库路径 在.pro文件中添加 INCLUDEPATH += D:/opencv/OpencvMingw/opencv310/include LIBS += D:/openc ...