A sequence of appearances refers to a series of integers with the following characteristics:
D, D1, d111, d113, d11231, d112213111, ...
It never equals the number D of 1, and the n+1 item of the sequence is a description of the nth item. For example, the 2nd means that the 1th item has 1 d, so it is D1;
The 2nd item is 1 d (corresponds to D1) and a 1 (corresponding to 11), so the 3rd item is d111. Another example is the 4th item is d113, its description is a d,2 1, a 3,
So the next item is d11231. Of course this definition is also set for d = 1. The subject asks you to calculate the nth of the appearance sequence for any given number d.
Input format:
Enter the first line to give an integer d in the [0,9] range, and a positive integer N (≤40), separated by a space.
Output format:
The nth item in a row that gives the appearance of an array of number d.
Input Sample:
1 8
Sample output:
1123123111
My solution is mainly to construct a string d=d+ ".", so as to ensure that when judging d[i]==d[i+1],
The subscript does not overflow, and avoids the "." The problem is equal to the number.
Then the iterative method can be used to do it. This problem began to think for a long time, want to use a recursive method to solve,
But I think it's too complicated, and then after a while, find an example and analyze it separately.
D,n =input (). Split ()defFun (d): D= d+"."s=""Count= 1 forIinchRange (len (D)):ifD[i]==d[i+1]: Count+=1Else: S= s+d[i]+Str (count) Count= 1returnSN=1 whileTrue:ifn==Int (N): Break Else: D=Fun (D) n+=1PrintD
PAT 1084 appearance sequence Python solution