在给定两个变量x和y的情况下,不使用第三个变量就交换两个变量。
方法1(使用算术运算符)
这个想法是获得两个给定数字之一的和。然后可以使用总和和减去总和来交换数字。
C++
// C++ Program to swap two numbers without
// using temporary variable
#include
using namespace std;
int main()
{
int x = 10, y = 5;
// Code to swap 'x' and 'y'
x = x + y; // x now becomes 15
y = x - y; // y becomes 10
x = x - y; // x becomes 5
cout << "After Swapping: x =" << x << ", y=" << y;
}
// This code is contributed by mohit kumar.
C
#include
int main()
{
int x = 10, y = 5;
// Code to swap 'x' and 'y'
x = x + y; // x now becomes 15
y = x - y; // y becomes 10
x = x - y; // x becomes 5
printf("After Swapping: x = %d, y = %d", x, y);
return 0;
}
Java
// Java Program to swap two numbers without
// using temporary variable
import java.io.*;
class Geeks {
public static void main(String a[])
{
int x = 10;
int y = 5;
x = x + y;
y = x - y;
x = x - y;
System.out.println("After swaping:"
+ " x = " + x + ", y = " + y);
}
}
// This code is contributed by Mayank Tyagi
Python3
x = 10
y = 5
# Code to swap 'x' and 'y'
# x now becomes 15
x = x + y
# y becomes 10
y = x - y
# x becomes 5
x = x - y
print("After Swapping: x =", x, " y =", y)
# This code is contributed
# by Sumit Sudhakar
C#
// Program to swap two numbers without
// using temporary variable
using System;
class GFG {
public static void Main()
{
int x = 10;
int y = 5;
x = x + y;
y = x - y;
x = x - y;
Console.WriteLine("After swapping: x = " + x
+ ", y = " + y);
}
}
// This code is contributed by Sam007
PHP
Javascript
C++
// C++ Program to swap two numbers
// without using temporary variable
#include
using namespace std;
int main()
{//NOTE - for this code to work in a generalised sense, y !- 0 to prevent zero division
int x = 10, y = 5;
// Code to swap 'x' and 'y'
x = x * y; // x now becomes 15
y = x / y; // y becomes 10
x = x / y; // x becomes 5
cout << "After Swapping: x =" << x << ", y=" << y;
}
// This code is contributed by mohit kumar.
C
// C Program to swap two numbers
// without using temporary variable
#include
int main()
{
int x = 10, y = 5;
// Code to swap 'x' and 'y'
x = x * y; // x now becomes 50
y = x / y; // y becomes 10
x = x / y; // x becomes 5
printf("After Swapping: x = %d, y = %d", x, y);
return 0;
}
Java
// Java Program to swap two numbers
// without using temporary variable
import java.io.*;
class GFG {
public static void main(String[] args)
{
int x = 10;
int y = 5;
// Code to swap 'x' and 'y'
x = x * y; // x now becomes 50
y = x / y; // y becomes 10
x = x / y; // x becomes 5
System.out.println("After swaping:"
+ " x = " + x + ", y = " + y);
}
}
// This code is contributed by ajit
Python3
# Python3 program to
# swap two numbers
# without using
# temporary variable
x = 10
y = 5
# code to swap
# 'x' and 'y'
# x now becomes 50
x = x * y
# y becomes 10
y = x // y;
# x becomes 5
x = x // y;
print("After Swapping: x =",
x, " y =", y);
# This code is contributed
# by @ajit
C#
// C# Program to swap two
// numbers without using
// temporary variable
using System;
class GFG {
static public void Main()
{
int x = 10;
int y = 5;
// Code to swap 'x' and 'y'
x = x * y; // x now becomes 50
y = x / y; // y becomes 10
x = x / y; // x becomes 5
Console.WriteLine("After swaping:"
+ " x = " + x + ", y = " + y);
}
}
// This code is contributed by ajit.
PHP
Javascript
C++
// C++ code to swap using XOR
#include
using namespace std;
int main()
{
int x = 10, y = 5;
// Code to swap 'x' (1010) and 'y' (0101)
x = x ^ y; // x now becomes 15 (1111)
y = x ^ y; // y becomes 10 (1010)
x = x ^ y; // x becomes 5 (0101)
cout << "After Swapping: x =" << x << ", y=" << y;
return 0;
}
// This code is contributed by mohit kumar.
C
// C code to swap using XOR
#include
int main()
{
int x = 10, y = 5;
// Code to swap 'x' (1010) and 'y' (0101)
x = x ^ y; // x now becomes 15 (1111)
y = x ^ y; // y becomes 10 (1010)
x = x ^ y; // x becomes 5 (0101)
printf("After Swapping: x = %d, y = %d", x, y);
return 0;
}
Java
// Java code to swap using XOR
import java.io.*;
public class GFG {
public static void main(String a[])
{
int x = 10;
int y = 5;
// Code to swap 'x' (1010) and 'y' (0101)
x = x ^ y; // x now becomes 15 (1111)
y = x ^ y; // y becomes 10 (1010)
x = x ^ y; // x becomes 5 (0101)
System.out.println("After swap: x = "
+ x + ", y = " + y);
}
}
// This code is contributed by Mayank Tyagi
Python3
# Python3 code to swap using XOR
x = 10
y = 5
# Code to swap 'x' and 'y'
x = x ^ y; # x now becomes 15 (1111)
y = x ^ y; # y becomes 10 (1010)
x = x ^ y; # x becomes 5 (0101)
print ("After Swapping: x = ", x, " y =", y)
# This code is contributed by
# Sumit Sudhakar
C#
// C# program to swap using XOR
using System;
class GFG {
public static void Main()
{
int x = 10;
int y = 5;
// Code to swap 'x' (1010)
// and 'y' (0101)
// x now becomes 15 (1111)
x = x ^ y;
// y becomes 10 (1010)
y = x ^ y;
// x becomes 5 (0101)
x = x ^ y;
Console.WriteLine("After swap: x = " + x + ", y = " + y);
}
}
// This code is contributed by ajit
PHP
Javascript
C++
#include
using namespace std;
void swap(int* xp, int* yp)
{
*xp = *xp ^ *yp;
*yp = *xp ^ *yp;
*xp = *xp ^ *yp;
}
// Driver code
int main()
{
int x = 10;
swap(&x, &x);
cout << "After swap(&x, &x): x = " << x;
return 0;
}
// This code is contributed by rathbhupendra
C
#include
void swap(int* xp, int* yp)
{
*xp = *xp ^ *yp;
*yp = *xp ^ *yp;
*xp = *xp ^ *yp;
}
int main()
{
int x = 10;
swap(&x, &x);
printf("After swap(&x, &x): x = %d", x);
return 0;
}
Java
class GFG {
static void swap(int[] xp, int[] yp)
{
xp[0] = xp[0] ^ yp[0];
yp[0] = xp[0] ^ yp[0];
xp[0] = xp[0] ^ yp[0];
}
// Driver code
public static void main(String[] args)
{
int[] x = { 10 };
swap(x, x);
System.out.println("After swap(&x, &x): x = "
+ x[0]);
}
}
// This code is contributed by Rajput-Ji
Python3
def swap(xp, yp):
xp[0] = xp[0] ^ yp[0]
yp[0] = xp[0] ^ yp[0]
xp[0] = xp[0] ^ yp[0]
# Driver code
x = [10]
swap(x, x)
print("After swap(&x, &x): x = ", x[0])
# This code is contributed by SHUBHAMSINGH10
C#
// C# program to implement
// the above approach
using System;
class GFG {
static void swap(int[] xp, int[] yp)
{
xp[0] = xp[0] ^ yp[0];
yp[0] = xp[0] ^ yp[0];
xp[0] = xp[0] ^ yp[0];
}
// Driver code
static void Main()
{
int[] x = { 10 };
swap(x, x);
Console.WriteLine("After swap(&x,"
+ "&x): x = " + x[0]);
}
}
// This code is contributed by divyeshrabadiya07
PHP
Javascript
C++
#include
using namespace std;
void swap(int* xp, int* yp)
{
// Check if the two addresses are same
if (xp == yp)
return;
*xp = *xp + *yp;
*yp = *xp - *yp;
*xp = *xp - *yp;
}
// Driver Code
int main()
{
int x = 10;
swap(&x, &x);
cout << "After swap(&x, &x): x = " << x;
return 0;
}
// This code is contributed by rathbhupendra
C
#include
void swap(int* xp, int* yp)
{
if (xp == yp) // Check if the two addresses are same
return;
*xp = *xp + *yp;
*yp = *xp - *yp;
*xp = *xp - *yp;
}
int main()
{
int x = 10;
swap(&x, &x);
printf("After swap(&x, &x): x = %d", x);
return 0;
}
Java
// Java program of above approach
class GFG {
static void swap(int xp, int yp)
{
if (xp == yp) // Check if the two addresses are same
return;
xp = xp + yp;
yp = xp - yp;
xp = xp - yp;
}
// Driver code
public static void main(String[] args)
{
int x = 10;
swap(x, x);
System.out.println("After swap(&x, &x): x = " + x);
}
}
// This code is Contributed by Code_Mech.
Python3
# Python3 program of above approach
def swap(xp, yp):
# Check if the two addresses are same
if (xp[0] == yp[0]):
return
xp[0] = xp[0] + yp[0]
yp[0] = xp[0] - yp[0]
xp[0] = xp[0] - yp[0]
# Driver Code
x = [10]
swap(x, x)
print("After swap(&x, &x): x = ", x[0])
# This code is contributed by SHUBHAMSINGH10
C#
// C# program of above approach
using System;
class GFG {
static void swap(int xp, int yp)
{
if (xp == yp) // Check if the two addresses are same
return;
xp = xp + yp;
yp = xp - yp;
xp = xp - yp;
}
// Driver code
public static void Main()
{
int x = 10;
swap(x, x);
Console.WriteLine("After swap(&x, &x): x = " + x);
}
}
// This code is Contributed by Code_Mech.
PHP
Javascript
C++
// C++ program to swap two numbers.
// Including header file.
#include
using namespace std;
// Function to swap the numbers.
void swap(int& a, int& b)
{
// same as a = a + b
a = (a & b) + (a | b);
// same as b = a - b
b = a + (~b) + 1;
// same as a = a - b
a = a + (~b) + 1;
}
// Driver Code
int main()
{
int a = 5, b = 10;
// Function Call
swap(a, b);
cout << "After swapping: a = " << a << ", b = " << b;
return 0;
}
// This code is contributed by yashbeersingh42
Java
// Java program to swap two numbers
import java.io.*;
class GFG {
public static void swap(int a, int b)
{
// same as a = a + b
a = (a & b) + (a | b);
// same as b = a - b
b = a + (~b) + 1;
// same as a = a - b
a = a + (~b) + 1;
System.out.print("After swapping: a = " + a
+ ", b = " + b);
}
public static void main(String[] args)
{
int a = 5, b = 10;
// Function Call
swap(a, b);
}
}
// This code is contributed by yashbeersingh42
Python3
# Python3 program to swap two numbers
# Function to swap the numbers
def swap(a, b):
# Same as a = a + b
a = (a & b) + (a | b)
# Same as b = a - b
b = a + (~b) + 1
# Same as a = a - b
a = a + (~b) + 1
print("After Swapping: a = ", a, ", b = ", b)
# Driver code
a = 5
b = 10
# Function call
swap(a, b)
# This code is contributed by bunnyram19
C#
// C# program to swap two numbers
using System;
class GFG {
static void swap(int a, int b)
{
// same as a = a + b
a = (a & b) + (a | b);
// same as b = a - b
b = a + (~b) + 1;
// same as a = a - b
a = a + (~b) + 1;
Console.Write("After swapping: a = " + a
+ ", b = " + b);
}
static void Main()
{
int a = 5, b = 10;
// Function Call
swap(a, b);
}
}
// This code is contributed by divyesh072019
Javascript
After Swapping: x =5, y=10
乘法和除法也可以用于交换。
C++
// C++ Program to swap two numbers
// without using temporary variable
#include
using namespace std;
int main()
{//NOTE - for this code to work in a generalised sense, y !- 0 to prevent zero division
int x = 10, y = 5;
// Code to swap 'x' and 'y'
x = x * y; // x now becomes 15
y = x / y; // y becomes 10
x = x / y; // x becomes 5
cout << "After Swapping: x =" << x << ", y=" << y;
}
// This code is contributed by mohit kumar.
C
// C Program to swap two numbers
// without using temporary variable
#include
int main()
{
int x = 10, y = 5;
// Code to swap 'x' and 'y'
x = x * y; // x now becomes 50
y = x / y; // y becomes 10
x = x / y; // x becomes 5
printf("After Swapping: x = %d, y = %d", x, y);
return 0;
}
Java
// Java Program to swap two numbers
// without using temporary variable
import java.io.*;
class GFG {
public static void main(String[] args)
{
int x = 10;
int y = 5;
// Code to swap 'x' and 'y'
x = x * y; // x now becomes 50
y = x / y; // y becomes 10
x = x / y; // x becomes 5
System.out.println("After swaping:"
+ " x = " + x + ", y = " + y);
}
}
// This code is contributed by ajit
Python3
# Python3 program to
# swap two numbers
# without using
# temporary variable
x = 10
y = 5
# code to swap
# 'x' and 'y'
# x now becomes 50
x = x * y
# y becomes 10
y = x // y;
# x becomes 5
x = x // y;
print("After Swapping: x =",
x, " y =", y);
# This code is contributed
# by @ajit
C#
// C# Program to swap two
// numbers without using
// temporary variable
using System;
class GFG {
static public void Main()
{
int x = 10;
int y = 5;
// Code to swap 'x' and 'y'
x = x * y; // x now becomes 50
y = x / y; // y becomes 10
x = x / y; // x becomes 5
Console.WriteLine("After swaping:"
+ " x = " + x + ", y = " + y);
}
}
// This code is contributed by ajit.
的PHP
Java脚本
After Swapping: x =5, y=10
方法2(使用按位XOR)
按位XOR运算符可用于交换两个变量。两个数字x和y的XOR返回一个数字,其中x和y的位不同时,所有位都为1。例如,10(在Binary 1010中)和5(在Binary 0101中)的XOR为1111,而7(0111)和5(0101)的XOR为(0010)。
C++
// C++ code to swap using XOR
#include
using namespace std;
int main()
{
int x = 10, y = 5;
// Code to swap 'x' (1010) and 'y' (0101)
x = x ^ y; // x now becomes 15 (1111)
y = x ^ y; // y becomes 10 (1010)
x = x ^ y; // x becomes 5 (0101)
cout << "After Swapping: x =" << x << ", y=" << y;
return 0;
}
// This code is contributed by mohit kumar.
C
// C code to swap using XOR
#include
int main()
{
int x = 10, y = 5;
// Code to swap 'x' (1010) and 'y' (0101)
x = x ^ y; // x now becomes 15 (1111)
y = x ^ y; // y becomes 10 (1010)
x = x ^ y; // x becomes 5 (0101)
printf("After Swapping: x = %d, y = %d", x, y);
return 0;
}
Java
// Java code to swap using XOR
import java.io.*;
public class GFG {
public static void main(String a[])
{
int x = 10;
int y = 5;
// Code to swap 'x' (1010) and 'y' (0101)
x = x ^ y; // x now becomes 15 (1111)
y = x ^ y; // y becomes 10 (1010)
x = x ^ y; // x becomes 5 (0101)
System.out.println("After swap: x = "
+ x + ", y = " + y);
}
}
// This code is contributed by Mayank Tyagi
Python3
# Python3 code to swap using XOR
x = 10
y = 5
# Code to swap 'x' and 'y'
x = x ^ y; # x now becomes 15 (1111)
y = x ^ y; # y becomes 10 (1010)
x = x ^ y; # x becomes 5 (0101)
print ("After Swapping: x = ", x, " y =", y)
# This code is contributed by
# Sumit Sudhakar
C#
// C# program to swap using XOR
using System;
class GFG {
public static void Main()
{
int x = 10;
int y = 5;
// Code to swap 'x' (1010)
// and 'y' (0101)
// x now becomes 15 (1111)
x = x ^ y;
// y becomes 10 (1010)
y = x ^ y;
// x becomes 5 (0101)
x = x ^ y;
Console.WriteLine("After swap: x = " + x + ", y = " + y);
}
}
// This code is contributed by ajit
的PHP
Java脚本
After Swapping: x =5, y=10
上述方法存在的问题
1)如果一个数字为0,则乘积和除法将不起作用,因为乘积变为0,而与另一个数字无关。
2)两种算术解决方案都可能导致算术溢出。如果x和y太大,则加法和乘法可能超出整数范围。
3)当我们使用指向变量的指针并进行函数交换时,当两个指针都指向同一个变量时,上述所有方法都会失败。让我们看一下如果两种情况都指向同一个变量,在这种情况下会发生什么。
//基于按位XOR的方法
x = x ^ x; // x变为0
x = x ^ x; // x保持为0
x = x ^ x; // x保持为0
//基于算术的方法
x = x + x; // x变成2x
x = x – x; // x变为0
x = x – x; // x保持为0
让我们看下面的程序。
C++
#include
using namespace std;
void swap(int* xp, int* yp)
{
*xp = *xp ^ *yp;
*yp = *xp ^ *yp;
*xp = *xp ^ *yp;
}
// Driver code
int main()
{
int x = 10;
swap(&x, &x);
cout << "After swap(&x, &x): x = " << x;
return 0;
}
// This code is contributed by rathbhupendra
C
#include
void swap(int* xp, int* yp)
{
*xp = *xp ^ *yp;
*yp = *xp ^ *yp;
*xp = *xp ^ *yp;
}
int main()
{
int x = 10;
swap(&x, &x);
printf("After swap(&x, &x): x = %d", x);
return 0;
}
Java
class GFG {
static void swap(int[] xp, int[] yp)
{
xp[0] = xp[0] ^ yp[0];
yp[0] = xp[0] ^ yp[0];
xp[0] = xp[0] ^ yp[0];
}
// Driver code
public static void main(String[] args)
{
int[] x = { 10 };
swap(x, x);
System.out.println("After swap(&x, &x): x = "
+ x[0]);
}
}
// This code is contributed by Rajput-Ji
Python3
def swap(xp, yp):
xp[0] = xp[0] ^ yp[0]
yp[0] = xp[0] ^ yp[0]
xp[0] = xp[0] ^ yp[0]
# Driver code
x = [10]
swap(x, x)
print("After swap(&x, &x): x = ", x[0])
# This code is contributed by SHUBHAMSINGH10
C#
// C# program to implement
// the above approach
using System;
class GFG {
static void swap(int[] xp, int[] yp)
{
xp[0] = xp[0] ^ yp[0];
yp[0] = xp[0] ^ yp[0];
xp[0] = xp[0] ^ yp[0];
}
// Driver code
static void Main()
{
int[] x = { 10 };
swap(x, x);
Console.WriteLine("After swap(&x,"
+ "&x): x = " + x[0]);
}
}
// This code is contributed by divyeshrabadiya07
的PHP
Java脚本
After swap(&x, &x): x = 0
在许多标准算法中可能需要将变量本身交换。例如,请参见QuickSort的此实现,我们可以在其中交换变量。通过在交换之前设置条件可以避免上述问题。
C++
#include
using namespace std;
void swap(int* xp, int* yp)
{
// Check if the two addresses are same
if (xp == yp)
return;
*xp = *xp + *yp;
*yp = *xp - *yp;
*xp = *xp - *yp;
}
// Driver Code
int main()
{
int x = 10;
swap(&x, &x);
cout << "After swap(&x, &x): x = " << x;
return 0;
}
// This code is contributed by rathbhupendra
C
#include
void swap(int* xp, int* yp)
{
if (xp == yp) // Check if the two addresses are same
return;
*xp = *xp + *yp;
*yp = *xp - *yp;
*xp = *xp - *yp;
}
int main()
{
int x = 10;
swap(&x, &x);
printf("After swap(&x, &x): x = %d", x);
return 0;
}
Java
// Java program of above approach
class GFG {
static void swap(int xp, int yp)
{
if (xp == yp) // Check if the two addresses are same
return;
xp = xp + yp;
yp = xp - yp;
xp = xp - yp;
}
// Driver code
public static void main(String[] args)
{
int x = 10;
swap(x, x);
System.out.println("After swap(&x, &x): x = " + x);
}
}
// This code is Contributed by Code_Mech.
Python3
# Python3 program of above approach
def swap(xp, yp):
# Check if the two addresses are same
if (xp[0] == yp[0]):
return
xp[0] = xp[0] + yp[0]
yp[0] = xp[0] - yp[0]
xp[0] = xp[0] - yp[0]
# Driver Code
x = [10]
swap(x, x)
print("After swap(&x, &x): x = ", x[0])
# This code is contributed by SHUBHAMSINGH10
C#
// C# program of above approach
using System;
class GFG {
static void swap(int xp, int yp)
{
if (xp == yp) // Check if the two addresses are same
return;
xp = xp + yp;
yp = xp - yp;
xp = xp - yp;
}
// Driver code
public static void Main()
{
int x = 10;
swap(x, x);
Console.WriteLine("After swap(&x, &x): x = " + x);
}
}
// This code is Contributed by Code_Mech.
的PHP
Java脚本
After swap(&x, &x): x = 10
方法3(按位运算运算符和算术运算运算符)
这个想法与方法1中讨论的想法相同,但是使用按位加法和减法进行交换。
下面是上述方法的实现。
C++
// C++ program to swap two numbers.
// Including header file.
#include
using namespace std;
// Function to swap the numbers.
void swap(int& a, int& b)
{
// same as a = a + b
a = (a & b) + (a | b);
// same as b = a - b
b = a + (~b) + 1;
// same as a = a - b
a = a + (~b) + 1;
}
// Driver Code
int main()
{
int a = 5, b = 10;
// Function Call
swap(a, b);
cout << "After swapping: a = " << a << ", b = " << b;
return 0;
}
// This code is contributed by yashbeersingh42
Java
// Java program to swap two numbers
import java.io.*;
class GFG {
public static void swap(int a, int b)
{
// same as a = a + b
a = (a & b) + (a | b);
// same as b = a - b
b = a + (~b) + 1;
// same as a = a - b
a = a + (~b) + 1;
System.out.print("After swapping: a = " + a
+ ", b = " + b);
}
public static void main(String[] args)
{
int a = 5, b = 10;
// Function Call
swap(a, b);
}
}
// This code is contributed by yashbeersingh42
Python3
# Python3 program to swap two numbers
# Function to swap the numbers
def swap(a, b):
# Same as a = a + b
a = (a & b) + (a | b)
# Same as b = a - b
b = a + (~b) + 1
# Same as a = a - b
a = a + (~b) + 1
print("After Swapping: a = ", a, ", b = ", b)
# Driver code
a = 5
b = 10
# Function call
swap(a, b)
# This code is contributed by bunnyram19
C#
// C# program to swap two numbers
using System;
class GFG {
static void swap(int a, int b)
{
// same as a = a + b
a = (a & b) + (a | b);
// same as b = a - b
b = a + (~b) + 1;
// same as a = a - b
a = a + (~b) + 1;
Console.Write("After swapping: a = " + a
+ ", b = " + b);
}
static void Main()
{
int a = 5, b = 10;
// Function Call
swap(a, b);
}
}
// This code is contributed by divyesh072019
Java脚本
After swapping: a = 10, b = 5
要了解有关在一行中交换两个变量的更多信息,请单击此处。