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 ...
随机推荐
- 何凯文每日一句打卡||DAY9
- Java迭代器用法
public class Test01 { public static void main(String[] args) { List list = new ArrayList(); list.add ...
- [转载]Brackets - 强大免费的开源跨平台Web前端开发工具IDE (HTML/CSS/Javascript代码编辑器)
http://brackets.io/ Brackets 是一个免费.开源且跨平台的 HTML/CSS/JavaScript 前端 WEB 集成开发环境 (IDE工具).该项目由 Adobe 创建和维 ...
- SOCKET中send和recv函数工作原理与注意点
https://blog.csdn.net/rankun1/article/details/50488989
- 10.29训练赛第一场B题
题目大意:有n个队伍之间比赛,每两个队伍之间都有一场比赛,因此一共有n(n-1) / 2场比赛,但是这里丢失了一场比赛的记录,现在让你通过n(n-1) /2 -1场仍然存在的比赛记录来判断丢失的那条比 ...
- tensorflow随机张量创建
TensorFlow 有几个操作用来创建不同分布的随机张量.注意随机操作是有状态的,并在每次评估时创建新的随机值. 下面是一些相关的函数的介绍: tf.random_normal 从正态分布中输出随机 ...
- 一份最中肯的Java学习路线+资源分享(拒绝傻逼式分享)
这是一篇针对Java初学者,或者说在Java学习路线上出了一些问题(不知道该学什么.不知道整体的学习路线是什么样的) 第一步:Java基础(一个月左右) 推荐视频: 下面的是黑马内部视频,我比较推荐的 ...
- 001_软件waf
一.优秀的软件waf开源软件 <1>openwaf介绍 http://www.oschina.net/p/openwaf http://git.oschina.net/miracleqi ...
- nor flash 和 nand flash
NOR Flash是很常见的一种存储芯片,数据掉电不会丢失,支持Execut On Chip,即程序可以直接在FLASH片内执行(这意味着存储在NOR FLash上的程序不需要复制到RAM就可以直接运 ...
- andriod 自定义来电界面功能
由于近期所做一个项目需要做类似于“来电秀”的功能,所以上网搜索了一些相关资料,加上自己的一些想法,做了一个Demo.一下是我对该功能实现的一些想法,不对的地方欢迎各位指出.最后我会给出Demo 的源代 ...