Answer the question
In order to leave comments, you need to log in
Determine the winner of the KNB?
Let there be the following designations for playing stone paper scissors
1 - stone
2 - scissors
3 - paper
It is necessary to determine the winner, by a pair of numbers, for example. (1, 3) is a loss, and (1, 2) is a win.
The article has a way through the remainder of the division. But I don't quite understand why it produces such results.
Here is an excerpt from the article:
0 - well (stone)
1 - scissors 2
- paper
. To determine the winner in a pair, we compare the numbers. (0 - 1) % 3 = 2, then the person lost, (0 - 2) % 3 = 1, then the person won, (2 - 2) % 3 = 0, a draw.
Answer the question
In order to leave comments, you need to log in
(-1) % 3 = 2
This is the mathematical definition of modulo residues. All numbers that differ by n have the same remainder modulo n. The embarrassment is due to the fact that the modulo operation does not work in many programming languages. For negative numbers, it will give a negative value, although you can add n to it to get the correct modulus - a number from 0 to n-1.
Therefore, when implementing, you can do (a-b+3)%3
Or transform so that there is no subtraction:b == (a+1)%3
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question