📜  编程难题(在没有任何控制语句的情况下分配值)

📅  最后修改于: 2021-05-30 11:54:47             🧑  作者: Mango

给定四个整数“ a”,“ b”,“ y”和“ x”,其中“ x”只能是零或一。您的任务如下:

  • 如果“ x”为零,则将值“ a”分配给变量“ y”
  • 如果“ x”是一个,则将值“ b”分配给变量“ y”。

不允许使用任何条件运算符(包括三元运算符)。
例子 :

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

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

这个想法是创建一个大小为2的数组,其中第一个元素为’a’,第二个元素为’b’。我们使用x作为数组中的索引。

C++
// CPP program to pick a value among two
// according to value of a third variable.
#include 
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;
}


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


Javascript


C++
// C++ program to pick a value among two
// according to value of a third variable.
#include 
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;
}


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


输出 :

3

替代解决方案:

C++

// C++ program to pick a value among two
// according to value of a third variable.
#include 
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;
}

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

Java脚本


输出 :

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