4
The problem:
With a very creative name, the Protagonist is in his first episode of its incredible saga!
In an afternoon any Z, the Protagonist’s best friend, challenged the even for a super fun challenge.
Z writes two whole numbers A and B on a white board and asks if the Protagonist can make the two integers equal using the minimum number of moves.
In a movement, the Protagonist can make one of the following actions:
Choose one of the numbers and add 3 to it (x = x + 3).
Choose one of the numbers and add 2 to it (x = x + 2).
Knowing this, as the Protagonist knows that you are very good he asked so that you could help him on this mission and tell him how many minimum of moves he needs to make both of them numbers become equal. You receive two integers (1 <= A, B <= 10 5). A single integer N, equivalent to quantity, shall be printed minimum of movements the Protagonist needs to make.
My attempt:
The entry there is standard of the question because it informs the two numbers on the same line, I created a list already adding them converted to integers pq they were previously strings.
I used the function max()
and min()
to take the respective values (higher and lower).
The variables are named because it was still testing, c1
and c2
are the counters of the sum movements that I make in the smallest number, a
and b
are two temporary variables that I created to take the value of the smallest number.
Then I try to create a list of numbers by jumping from 2/3 until I reach the highest number, incrementing the counter every time. At the end I compare which counter is smaller, because it would be the final answer, the first if, was like this because in some cases the counter of both was equal ex: when the input was 6 and 8.
entrada = str(input()).split()
numeros = []
for i in entrada:
numeros.append(int(i))
B = max(numeros)
A = min(numeros)
div, mod = divmod(B-A, 3)
if mod != 0:
div += 3 - mod
print(f'{div}')
Could you describe in text what logic you tried to implement?
– Woss
Nice and easy, you’re missing the point. You get two numbers, in the smallest you go the sum of three in three, in the largest you go adding two in two. You want to know how many sums will be needed for the values to be equal?
– Augusto Vasques
You can put all this directly in the question to [Edit]. Better than leaving it in the comments.
– Woss
@Augustovasques, I’m getting two numbers, so I got a bigger one and a smaller one. the character in the question has two options: add 3 to the number or add 2 to the number until they are equal (highest and lowest number of the input)
– user217173
@Woss all right.
– user217173
@Augustovasques later I have to say which is the smallest movement I make to match the numbers (using 3 or 2)
– user217173
Now it’s clear, I’m just gonna shake it up a little bit to make it easier to read.
– Augusto Vasques
Taking advantage,
input
already returns a string, so dostr(input())
is unnecessary. And if you are already absolutely sure that there will always be 2 numbers on the same line, you can do:a, b = map(int, input().split())
– hkotsubo
John, I reversed your last issue because it’s important that the question has the code you tried. Learn more at: https://pt.meta.stackoverflow.com/a/5484/112052
– hkotsubo