假设变量x只能有两个可能的值a和b,并且您希望将x的值分配给它的当前值。无需使用任何条件运算符即可高效地执行此操作。
注意:我们不允许检查x的当前值。
例子:
Input : a = 10, b = 15, x = a
Output : x = 15
Explanation x = 10, currently x has value of a (which is 10), we need to change it to 15.
Input : a = 9, b = 11, x = b
Output : x = 9
我们可以使用if条件解决此问题,但是我们不允许这样做。
if (x == a)
x = b;
else x = a;
我们本可以使用Ternary运算符,它还会检查x的当前值,并根据它分配新值。所以我们也不能使用这种方法
x = x == a ? b : a;
但是,我们不允许检查x的值,因此上述解决方案均无效。
解决方案1:仅使用算术运算运算符,我们可以执行此操作
x = a + b - x
这样,x的内容将在每次执行时在a和b之间交替
C++
// CPP program to change value of x
// according to its current value.
#include
using namespace std;
// Function to alternate the values
void alternate(int& a, int& b, int& x)
{
x = a + b - x;
}
// Main function
int main()
{
int a = -10;
int b = 15;
int x = a;
cout << "x is : " << x;
alternate(a, b, x);
cout << "\nAfter change ";
cout << "\nx is : " << x;
}
Java
// Java program to change value of x
// according to its current value.
import java.util.*;
class solution
{
// Function to alternate the values
static void alternate(int a, int b, int x)
{
x = a + b - x;
System.out.println("After change"+"\n"+" x is : "+x);
}
// Main function
public static void main(String args[])
{
int a = -10;
int b = 15;
int x = a;
System.out.println("x is : "+x);
alternate(a, b, x);
}
}
Python3
# Python3 program to change value
# of x according to its current value.
# Function to alternate the values
def alternate(a,b,x):
x = a+b-x
print("After change x is:",x)
# Driver code
if __name__=='__main__':
a = -10
b = 15
x = a
print("x is:",x)
alternate(a,b,x)
# This code is contributed by
# Shrikant13
C#
// C# program to change value of x
// according to its current value.
using System;
class gfg
{
// Function to alternate the values
public void alternate(ref int a, ref int b, ref int x)
//'ref' indicates the references
{
x = a + b - x;
}
}
// Main function
class geek
{
public static int Main()
{
gfg g = new gfg();
int a = -10;
int b = 15;
int x = a;
Console.WriteLine("x is : {0}" , x);
g.alternate(ref a, ref b, ref x);
Console.WriteLine ("After change ");
Console.WriteLine("x is : {0}", x);
return 0;
}
}
//This code is contributed by Soumik
PHP
C++
// CPP program to change value of x
// according to its current value.
#include
using namespace std;
// Function to alternate the values
void alternate(int& a, int& b, int& x)
{
x = a ^ b ^ x;
}
// Main function
int main()
{
int a = -10;
int b = 15;
int x = a;
cout << "x is : " << x;
alternate(a, b, x);
cout << "\nAfter exchange ";
cout << "\nx is : " << x;
return 0;
}
Java
// Java program to change value of x
// according to its current value.
class GFG {
// Function to alternate the values
static int alternate(int a, int b, int x) {
return x = a ^ b ^ x;
}
// Main function
public static void main(String[] args) {
int a = -10;
int b = 15;
int x = a;
System.out.print("x is : " + x);
x = alternate(a, b, x);
System.out.print("\nAfter exchange ");
System.out.print("\nx is : " + x);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to change value of x
# according to its current value.
# Function to alternate the values
def alternate(a, b, x):
x = a ^ b ^ x
print("After exchange")
print("x is", x)
# Driver code
a = -10
b = 15
x = a
print("x is", x)
alternate(a, b, x)
# This code is contributed
# by Shrikant13
C#
// C# program to change value of x
// according to its current value.
using System;
public class GFG {
// Function to alternate the values
static int alternate(int a, int b, int x) {
return x = a ^ b ^ x;
}
// Main function
public static void Main() {
int a = -10;
int b = 15;
int x = a;
Console.Write("x is : " + x);
x = alternate(a, b, x);
Console.Write("\nAfter exchange ");
Console.Write("\nx is : " + x);
}
}
/*This code is contributed by Rajput-Ji*/
PHP
输出:
x is : -10
After change
x is : 15
时间复杂度:此方法的时间复杂度为O(1)
空间复杂度:此方法的空间复杂度为O(1)
解决方案2:一种更好,更有效的方法是使用按位XOR操作。
x = a ^ b ^ x
C++
// CPP program to change value of x
// according to its current value.
#include
using namespace std;
// Function to alternate the values
void alternate(int& a, int& b, int& x)
{
x = a ^ b ^ x;
}
// Main function
int main()
{
int a = -10;
int b = 15;
int x = a;
cout << "x is : " << x;
alternate(a, b, x);
cout << "\nAfter exchange ";
cout << "\nx is : " << x;
return 0;
}
Java
// Java program to change value of x
// according to its current value.
class GFG {
// Function to alternate the values
static int alternate(int a, int b, int x) {
return x = a ^ b ^ x;
}
// Main function
public static void main(String[] args) {
int a = -10;
int b = 15;
int x = a;
System.out.print("x is : " + x);
x = alternate(a, b, x);
System.out.print("\nAfter exchange ");
System.out.print("\nx is : " + x);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to change value of x
# according to its current value.
# Function to alternate the values
def alternate(a, b, x):
x = a ^ b ^ x
print("After exchange")
print("x is", x)
# Driver code
a = -10
b = 15
x = a
print("x is", x)
alternate(a, b, x)
# This code is contributed
# by Shrikant13
C#
// C# program to change value of x
// according to its current value.
using System;
public class GFG {
// Function to alternate the values
static int alternate(int a, int b, int x) {
return x = a ^ b ^ x;
}
// Main function
public static void Main() {
int a = -10;
int b = 15;
int x = a;
Console.Write("x is : " + x);
x = alternate(a, b, x);
Console.Write("\nAfter exchange ");
Console.Write("\nx is : " + x);
}
}
/*This code is contributed by Rajput-Ji*/
的PHP
输出:
x is : -10
After exchange
x is : 15
时间复杂度:此方法的时间复杂度为O(1)
空间复杂度:此方法的空间复杂度为O(1)