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千题计划283:bzoj4516: [Sdoi2016]生成魔咒(后缀数组)
http://www.lydsy.com/JudgeOnline/problem.php?id=4516 考虑在后面新加一个字母产生的影响 假设是第i个 如果不考虑重复,那么会增加i个不同的字符串 考 ...
- Nginx学习总结
2017年2月23日, 星期四 Nginx学习总结 Nginx是目前比较主流的HTTP反向代理服务器(其企业版提供了基于TCP层的反向代理插件),对于构建大型分布式web应用,具有举足轻重的作用.简单 ...
- 项目引入非配置的文件,打成war包后测试报错的可能原因
写在前边 这阵子有点忙,开发一个微服务项目中读取配置文件的时候在本地测试是可以的,但是一到测试环境就报错,经查看发现是因为发布的时候是用的war包,使用java -jar xxx.war启动的,所以用 ...
- 一些js的小技巧
这里收集了一些编码上的小技巧,大家可以学习学习. 1.浮点转整型 使用|0快速转换 var a=(12.002)|0;//12 使用~~快速转换 ~取反运算符,2=0010,~2=1101,因为第一位 ...
- 第7月第27天 c++11 boost
1. #include <boost/asio.hpp> #include <cstdlib> #include <memory> namespace asio = ...
- 以后的博客将更新到自己的域名pythonsite.com,欢迎访问
以后的博客将更新到自己的域名pythonsite.com,欢迎访问
- 云计算-MapReduce
Hadoop示例程序WordCount详解及实例http://blog.csdn.net/xw13106209/article/details/6116323 hadoop中使用MapReduce编程 ...
- rpmbuild 构建rpm包时报错解决 error: Installed (but unpackaged) file(s) found:
解决的办法是找到 /usr/lib/rpm/macros 中%__check_files /usr/lib/rpm/check-files %{buildroot} 注释掉
- CasperJS API中文博客链接
http://www.cnblogs.com/reach296/tag/Casperjs/
- 多继承下的super()指向的不一定是直接父类
常规情况 class Base: def __init__(self): print('Base.__init__') class A(Base): def __init__(self): super ...