Write a condition that is true when only one of the numbers X, Y, and Z is a multiple of five.
At the same time, you cannot use the + - * / operators and comparison operators should not be more than 3x

How to do it?

Closed due to the fact that it was off topic by Nicolas Chabanovsky 8 Nov '16 at 5:33 .

  • Most likely, this question does not correspond to the subject of Stack Overflow in Russian, according to the rules described in the certificate .
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    And why suddenly "it is impossible to use the operators + - * / and comparison operators should not be more than 3"? Is this a practical problem or a learning task? - VladD
  • Learning task. - Dagmar
  • @Dagmar and with the bit shift did not consider the task? - Senior Pomidor
  • I am considering it. That’s what I’m doing now, but I still can’t understand. - Dagmar
  • @Dagmar Can you enter intermediate variables? - Vlad from Moscow

2 answers 2

If it is allowed to introduce additional variables to store intermediate results, the condition can be expressed as follows.

/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { public static void main (String[] args) throws java.lang.Exception { int x = 5, y = 6, z = 7; boolean a = x % 5 == 0, b = y % 5 == 0, c = z % 5 == 0; boolean result = !( a && b && c ) && ( a ^ b ^ c ); System.out.println( result ); x = 5; y = 5; z = 7; a = x % 5 == 0; b = y % 5 == 0; c = z % 5 == 0; result = !( a && b && c ) && ( a ^ b ^ c ); System.out.println( result ); x = 5; y = 5; z = 5; a = x % 5 == 0; b = y % 5 == 0; c = z % 5 == 0; result = !( a && b && c ) && ( a ^ b ^ c ); System.out.println( result ); } } 
     public static void main(String[] args) throws java.lang.Exception { int x = 9; int y = 15; int z = 6; /* не используем сравнения и арифметические операции в А, В, С, * они только чтобы формула не была длинной * Остаток от деления может быть 0, 1, 2, 3, 4, то есть * 000 * 001 * 010 * 011 * 100 * Сдвигаем биты до 2 разрядов, складываем по ИЛИ и умножаем по маске 1 * Результат будет равен 1, если остаток не равен 0 * */ int A = ((x % 5)|((x % 5) >> 1)|((x % 5) >> 2)) & 1; int B = ((y % 5)|((y % 5) >> 1)|((y % 5) >> 2)) & 1; int C = ((z % 5)|((z % 5) >> 1)|((z % 5) >> 2)) & 1; System.out.println(A + " " + B + " " + C); /* Комбинации, устраивающие нас * ABC * 0 1 1 * 1 0 1 * 1 1 0 * */ int result = (~A & B & C) | (A & ~B & C) | (A & B & ~C); System.out.println(result == 1); } // второе решение static boolean test(int x, int y, int z) { int[][] m = { { 1, 0, 0, 0, 0 }, { 2, 0, 0, 0, 0 }, { 4, 0, 0, 0, 0 } }; int res = m[0][Math.abs(x) % 5] | m[1][Math.abs(y) % 5] | m[2][Math.abs(z) % 5]; return ( ( res == 1 ) || ( res == 2 ) || ( res == 4) ); } 

    a source