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?
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?
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) ); } Source: https://ru.stackoverflow.com/questions/587902/
All Articles