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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值