Dados 4 enteros a, b, y y x, donde x puede asumir los valores de 0 o 1 únicamente. Se hace la siguiente pregunta:
If 'x' is 0, Assign value 'a' to variable 'y' Else (If 'x' is 1) Assign value 'b' to variable 'y'.
Nota: – No está permitido utilizar ningún operador condicional (incluido el operador ternario) ni ningún operador aritmético (+, -, *, /).
Ejemplos:
Input : a = 5 , b = 10, x = 1 Output : y = 10 Input : a = 5, b = 10 , x = 0 Output : y = 5
Preguntado en: Entrevista de Google
Solución 1:
Una idea es simplemente almacenar tanto ‘a’ como ‘b’ en una array en el índice 0 y 1, respectivamente. Luego almacene el valor en ‘y’ tomando ‘x’ como índice.
A continuación se muestra la implementación del enfoque anterior:
C++
// C/C++ program to assign value to y according // to value of x #include <iostream> using namespace std; // Function to assign value to y according // to value of x int assignValue(int a, int b, int x) { int y; int arr[2]; // Store both values in an array // value 'a' at 0th index arr[0] = a; // Value 'b' at 1th index arr[1] = b; // Assign value to 'y' taking 'x' as index y = arr[x]; return y; } // Driver code int main() { int a = 5; int b = 10; int x = 0; cout << "Value assigned to 'y' is " << assignValue(a, b, x); return 0; }
Java
// Java program to assign value to y according // to value of x public class GFG { static int assignValue(int a, int b, int x) { int y; int arr[] = new int[2]; // Store both values in an array // value 'a' at 0th index arr[0] = a; // Value 'b' at 1th index arr[1] = b; // Assign value to 'y' taking 'x' as index y = arr[x]; return y; } // Driver Method public static void main(String[] args) { int a = 5; int b = 10; int x = 0; System.out.println("Value assigned to 'y' is " + assignValue(a, b, x)); } }
Python3
# Python 3 program to assign value to # y according to value of x # Function to assign value to y # according to value of x def assignValue(a, b, x): arr = [0] * 2 # Store both values in an array # value 'a' at 0th index arr[0] = a # Value 'b' at 1th index arr[1] = b # Assign value to 'y' taking 'x' # as index y = arr[x] return y # Driver code if __name__ == "__main__": a = 5 b = 10 x = 0 print("Value assigned to 'y' is", assignValue(a, b, x)) # This code is contributed by ita_c
C#
// C# program to assign value to y according // to value of x using System; public class GFG { static int assignValue(int a, int b, int x) { int y; int[] arr = new int[2]; // Store both values in an array // value 'a' at 0th index arr[0] = a; // Value 'b' at 1th index arr[1] = b; // Assign value to 'y' taking 'x' // as index y = arr[x]; return y; } // Driver Code public static void Main() { int a = 5; int b = 10; int x = 0; Console.Write("Value assigned to " + "'y' is " + assignValue(a, b, x)); } } // This code is contributed by nitin mittal.
PHP
<?php // PHP program to assign value // to y according to value of x // Function to assign value // to y according to value of x function assignValue($a, $b, $x) { $y; $arr = array(0, 0); // Store both values in an array // value 'a' at 0th index $arr[0] = $a; // Value 'b' at 1th index $arr[1] = $b; // Assign value to 'y' // taking 'x' as index $y = $arr[$x]; return $y; } // Driver code $a = 5; $b = 10; $x = 0; echo "Value assigned to 'y' is " . assignValue($a, $b, $x); // This code is contributed by Anuj_67 ?>
Javascript
<script> // javascript program to assign value to y according // to value of x function assignValue(a , b , x) { var y; var arr = Array(2); // Store both values in an array // value 'a' at 0th index arr[0] = a; // Value 'b' at 1th index arr[1] = b; // Assign value to 'y' taking 'x' as index y = arr[x]; return y; } // Driver Method var a = 5; var b = 10; var x = 0; document.write("Value assigned to 'y' is " + assignValue(a, b, x)); // This code is contributed by todaysgaurav </script>
Value assigned to 'y' is 5
Solución 2:
Usando el operador AND bit a bit.
C++
// C/C++ program to assign value to y according // to value of x #include <iostream> using namespace std; // Driver Code int main() { int a = 5; int b = 10; int x = 1; int y; if (x & 1) y = b; else y = a; cout << "Value assigned to 'y' is " << y << endl; return 0; }
Java
// Java program to assign value to y according // to value of x import java.io.*; class GFG { // Driver Code public static void main (String[] args) { int a = 5; int b = 10; int x = 1; int y; if ((x & 1) != 0) y = b; else y = a; System.out.println("Value assigned to 'y' is " + y); } } // This code is contributed by avanitrachhadiya2155
Python3
# Python3 program to assign value to y # according to value of x # Driver Code a = 5 b = 10 x = 1 y = 0 if ((x & 1) != 0): y = b else: y = a print("Value assigned to 'y' is ", y) # This code is contributed by ab2127
C#
// C# program to assign value to y according // to value of x using System; public class GFG { // Driver Code static public void Main () { int a = 5; int b = 10; int x = 1; int y; if ((x & 1) != 0) y = b; else y = a; Console.WriteLine("Value assigned to 'y' is " + y); } } // This code is contributed by rag2127
Javascript
<script> // Javascript program to assign value to y according // to value of x // Driver Code var a = 5; var b = 10; var x = 1; var y; if ((x & 1) != 0) y = b; else y = a; document.write("Value assigned to 'y' is " + y); // This code contributed by Rajput-Ji </script>
Value assigned to 'y' is 10
Referencia: https://www.careercup.com/question?id=5135296679116800
Este artículo es una contribución de Sahil Chhabra (akku) . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA