📜  有条件地分配值,而无需使用条件和算术运算运算符

📅  最后修改于: 2021-05-30 15:48:48             🧑  作者: Mango

给定4个整数a,b,y和x,其中x只能是0和1。要求如下:

If 'x' is 0, 
   Assign value 'a' to variable 'y' 
Else (If 'x' is 1)
   Assign value 'b' to variable 'y'.

注意:–不允许使用任何条件运算符(包括三元运算符)或任何算术运算运算符(+,-,*,/)。

例子 :

Input :  a = 5 , b = 10, x = 1
Output :  y = 10

Input : a = 5, b = 10 , x = 0
Output :  y = 5

提问人:Google面试

解决方案1:

一个想法是简单地将“ a”和“ b”都分别存储在数组的第0个和第1个索引处。然后通过将“ x”作为索引将值存储到“ y”。

下面是上述方法的实现:

C++
// C/C++ program to assign value to y according
// to value of x
 
#include 
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));
    }
}


Python
# 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


Javascript


C++
// C/C++ program to assign value to y according
// to value of x
 
#include 
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


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


输出
Value assigned to 'y' is 5

解决方案2:

使用按位AND运算符。

C++

// C/C++ program to assign value to y according
// to value of x
 
#include 
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

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

Java脚本


输出
Value assigned to 'y' is 10

参考:https://www.careercup.com/question?id=5135296679116800

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”