Rompecabezas de programación (Asignar valor sin ninguna instrucción de control)

Dados cuatro números enteros ‘a’, ‘b’, ‘y’ y ‘x’, donde ‘x’ solo puede ser cero o uno. Tu tarea es la siguiente: 

  • Si ‘x’ es cero asigne el valor ‘a’ a la variable ‘y’
  • Si ‘x’ es uno, asigne el valor ‘b’ a la variable ‘y’.

No está permitido utilizar ningún operador condicional (incluido el operador ternario).

Ejemplos: 

Input  : a = 3, b = 7,  x = 1
Output : y = 7

Input : a = 3, b = 7, x = 0
Output : y = 3

La idea es crear una array de tamaño dos donde el primer elemento sea ‘a’ y el segundo elemento sea ‘b’. Usamos x como un índice en la array.

Implementación:

C++

// CPP program to pick a value among two
// according to value of a third variable.
#include <iostream>
using namespace std;
 
// Returns a if x is 0 and returns
// b if x is 1.
int assignValue(int a, int b, bool x)
{
    int arr[] = {a, b};
 
    return(arr[x]);
}
 
// Driver code
int main()
{
    int y = assignValue(3, 7, 0);
    cout << y;
    return 0;
}

C

// C program to pick a value among two
// according to value of a third variable.
#include <stdio.h>
#include <stdbool.h>
 
// Returns a if x is 0 and returns
// b if x is 1.
int assignValue(int a, int b, bool x)
{
    int arr[] = {a, b};
 
    return(arr[x]);
}
 
// Driver code
int main()
{
    int y = assignValue(3, 7, 0);
    printf("%d",y);
    return 0;
}
 
// This code is contributed by kothvvsaakash.

Java

// Java program to pick a value among two
// according to value of a third variable.
class GFG {
 
    // Returns a if x is 0 and returns
    // b if x is 1.
    static int assignValue(int a, int b, int x)
    {
        int arr[] = {a, b};
 
        return (arr[x]);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int y = assignValue(3, 7, 0);
        System.out.println(y);
    }
}
 
// This code is contributed by  Smitha Dinesh Semwal.

Python3

# Python 3 program to
# pick a value among two
# according to value
# of a third variable.
 
# Returns a if x
# is 0 and returns
# b if x is 1.
def assignValue(a, b, x):
 
    arr = [a, b]
    return(arr[x])
 
 
# Driver code
y = assignValue(3, 7, 0)
 
print(y)
 
# This code is contributed by
# Smitha Dinesh Semwal

C#

// C# program to pick a value among two
// according to value of a third variable.
using System;
 
public class GFG {
  
    // Returns a if x is 0 and returns
    // b if x is 1.
    static int assignValue(int a, int b, int x)
    {
        int []arr = {a, b};
  
        return (arr[x]);
    }
  
    // Driver code
    public static void Main()
    {
        int y = assignValue(3, 7, 0);
        Console.WriteLine(y);
    }
}
// This code is contributed by PrinciRaj1992

PHP

<?php
// PHP program to pick a value
// among two according to value
// of a third variable.
 
// Returns a if x is 0 and
// returns b if x is 1.
 
function assignValue($a, $b, $x)
{
    $arr = array($a, $b);
 
    return($arr[$x]);
}
 
// Driver code
$y = assignValue(3, 7, 0);
echo $y;
 
// This code is contributed by ajit
?>

Javascript

<script>
// javascript program to pick a value among two
// according to value of a third variable.   
// Returns a if x is 0 and returns
    // b if x is 1.
    function assignValue(a , b , x) {
        var arr = [ a, b ];
 
        return (arr[x]);
    }
 
    // Driver code
     
        var y = assignValue(3, 7, 0);
        document.write(y);
 
// This code is contributed by todaysgaurav
</script>
Producción

3

Complejidad temporal : O(1) 
Espacio auxiliar : O(1)

Solución alternativa: 

C++

// C++ program to pick a value among two
// according to value of a third variable.
#include <iostream>
using namespace std;
 
// Returns a if x is 0 and returns
// b if x is 1.
int assignValue(int a, int b, bool x)
{  
    return (1 - x)*a + x*b;
}
 
// Driver code
int main()
{
    int y = assignValue(3, 7, 0);
    cout << y;
    return 0;
}

C

// C program to pick a value among two
// according to value of a third variable.
#include <stdio.h>
#include <stdbool.h>
 
// Returns a if x is 0 and returns
// b if x is 1.
int assignValue(int a, int b, bool x)
{  
    return (1 - x)*a + x*b;
}
 
// Driver code
int main()
{
    int y = assignValue(3, 7, 0);
    printf("%d",y);
    return 0;
}
 
// This code is contributed by kothvvsaakash.

Java

// Java program to pick a value among two
// according to value of a third variable.
import java.io.*;
 
class GFG {
 
// Returns a if x is 0 and returns
// b if x is 1.
static int assignValue(int a, int b, int x)
{
    return (1 - x) * a + x * b;
}
         
// Driver code
public static void main (String[] args)
{
    int y = assignValue(3, 7, 0);
         
    System.out.println(y);
}
}
 
// This code is contributed by ShubhamCoder

Python3

# Python3 program to pick a value among two
# according to the value of a third variable.
 
# Returns a if x is 0 and returns
# b if x is 1.
def assignValue(a, b, x):
     
    return (1 - x) * a + x * b
     
# Driver code
y = assignValue(3, 7, 0)
print(y)
 
# This code is contributed by ShubhamCoder

C#

// C# program to pick a value among two
// according to value of a third variable.
using System;
 
class GFG {
 
// Returns a if x is 0 and returns
// b if x is 1.
static int assignValue(int a, int b, int x)
{
    return (1 - x) * a + x * b;
}
     
// Driver code
public static void Main()
{
    int y = assignValue(3, 7, 0);
     
    Console.WriteLine(y);
}
}
 
// This code is contributed by ShubhamCoder

Javascript

<script>
 
// Javascript program to pick a value among two
// according to value of a third variable.
 
    // Returns a if x is 0 and returns
    // b if x is 1.
    function assignValue(a , b , x) {
        return (1 - x) * a + x * b;
    }
 
    // Driver code
     
        var y = assignValue(3, 7, 0);
 
        document.write(y);
 
// This code contributed by Rajput-Ji
 
</script>
Producción

3

Complejidad temporal : O(1) 
Espacio auxiliar : O(1)

Gracias a Forrest Smith por sugerir la solución anterior.
Este artículo es una contribución de Maajid Bashir . 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. 

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

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *