A
A
angor202019-12-28 14:57:42
Algorithms
angor20, 2019-12-28 14:57:42

Have I made the right decision?

Executor Editor receives a string of numbers as input and converts it.
The editor can execute two commands, in both commands v and w stand for strings of numbers.
A) replace (v, w). This command replaces the first occurrence of v in a string with
w.
B) found (v). This command checks for the occurrence of the string v in a string.
Below is the program for the executor Editor.
FOUND (19) OR Found (299) OR Found (3999)
Replace (19, 2)
Replace (299, 3) Replace
(3999, 19 ) 100 consecutive digits 9. What string will be the result of applying the program to this string? AT
write down the received string in response.
My solution:
1999(100 digits) 9
First loop step:
19 -> 2
29999(99 digits) 9
Or 299 999(97 digits) 9
299 -> 3
3999(97 digits) 9
Or 3999 999(94 digits)
3999 - > 19
19 999 (94 digits)
Or
1999 (95 digits)
That is, for 1 pass of the cycle we need 5 nines.
Therefore, you need to separately look at the situation when there are 5 left.
199999
19 -> 2
29999
299 -> 3
399
And here it will fly off. The last replacement will not be performed and the cycle will end.
Answer: 399

Answer the question

In order to leave comments, you need to log in

1 answer(s)
X
xmoonlight, 2020-01-04
@xmoonlight

If the algorithm in the code gives the correct result on different inputs, most likely it is correct.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question