D
D
Denis Karasev2016-04-14 22:50:02
Java
Denis Karasev, 2016-04-14 22:50:02

Write a condition that is true?

Write a condition that is true when:
- only one of the numbers X, Y and Z is a multiple of five
I only thought of ((X%5 ==0) ^ (Y%5 ==0)) ^ (Z%5 = = 0)
Also, there is a condition, do not use more than 3 comparison operators

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Bykov, 2016-04-15
@dentantello

Consider an expression of the form
If X is a multiple of 5, then the value of the expression will be 1*2*3*4 = 24, otherwise it will be 0, because one of the factors will be equal to 0.
Given these considerations, the solution will be the following expression

(((X + 1) % 5) * ((X + 2) % 5) * ((X + 3) % 5) * ((X + 4) % 5) +
 ((Y + 1) % 5) * ((Y + 2) % 5) * ((Y + 3) % 5) * ((Y + 4) % 5) +
 ((Z + 1) % 5) * ((Z + 2) % 5) * ((Z + 3) % 5) * ((Z + 4) % 5)
) == 24

Because if all 3 values ​​are not multiples of 5 then the sum will be 0, if exactly one value is a multiple of 5 then the sum will be 24, and if more than one value is a multiple of 5 then the sum will be 48 or 72.
So as you can see , one comparison operator is sufficient.
PS: Your solution gives the wrong value in case all three values ​​are multiples of 5.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question