假设我们有四个变量 a、 b,c,d 我们想要表演 交换 以以下方式计算这些变量 a=b,b=c,c=d,d=a 不使用任何其他 第五个或临时变量
null
解决方案: 第一步。 交换 A. 和 B 不使用任何其他变量 a=a+b b=a-b a=a-b
第二步。 交换 B 和 C 不使用任何其他变量 b=b+c c=b–c b=b-c
第三步。 交换 C 和 D 不使用任何其他变量 c=c+d d=c–d c=c-d
例如:
Input : a = 1, b = 2, c = 3, d = 4Output :After swapping a = 2, b = 3, c = 4, d = 1Input :a = 6, b = 9, c = 10, d = 50Output : After swapping : a = 9, b = 10, c = 50, d = 6
先决条件: 在没有临时变量的情况下交换两个变量 , 在没有临时变量的情况下交换三个变量
CPP
// CPP program to swap 4 variables without // using temporary variable. #include <bits/stdc++.h> using namespace std; void swap( int a, int b, int c, int d) { // swapping a and b variables a = a + b; b = a - b; a = a - b; // swapping b and c variables b = b + c; c = b - c; b = b - c; // swapping c and d variables c = c + d; d = c - d; c = c - d; cout << "values after swapping are : " << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl; cout << "d = " << d << endl; } // Driver code int main() { // initialising variables int a = 1; int b = 2; int c = 3; int d = 4; cout << "Values before swapping are :" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; cout << "c = " << c << endl; cout << "d = " << d << endl << endl; // Function call swap(a, b, c, d); return 0; } |
JAVA
// Java program to swap 4 variables // without using temporary variable. class GFG { static void swap( int a, int b, int c, int d) { // swapping a and b variables a = a + b; b = a - b; a = a - b; // swapping b and c variables b = b + c; c = b - c; b = b - c; // swapping c and d variables c = c + d; d = c - d; c = c - d; System.out.println( "values after " + "swapping are : " ); System.out.println( "a = " + a); System.out.println( "b = " + b); System.out.println( "c = " + c); System.out.println( "d = " + d); } // Driver code public static void main(String[] args) { // initialising variables int a = 1 ; int b = 2 ; int c = 3 ; int d = 4 ; System.out.println( "values before " + "swapping are : " ); System.out.println( "a = " + a); System.out.println( "b = " + b); System.out.println( "c = " + c); System.out.println( "d = " + d); System.out.println( "" ); // Function call swap(a, b, c, d); } } // This code is contributed by Smitha. |
Python3
# Python 3 program to swap 4 variables # without using temporary variable. def swap(a, b, c, d): # swapping a and b variables a = a + b b = a - b a = a - b # swapping b and c variables b = b + c c = b - c b = b - c # swapping c and d variables c = c + d d = c - d c = c - d print ( "values after swapping are : " ) print ( "a = " , a) print ( "b = " , b) print ( "c = " , c) print ( "d = " , d) # Driver code # initialising variables a = 1 b = 2 c = 3 d = 4 print ( "values before swapping are : " ) print ( "a = " , a) print ( "b = " , b) print ( "c = " , c) print ( "d = " , d) print ("") # Function call swap(a, b, c, d) # This code is contributed by Smitha. |
C#
// C# program to swap 4 variables // without using temporary variable. using System; class GFG { static void swap( int a, int b, int c, int d) { // swapping a and b variables a = a + b; b = a - b; a = a - b; // swapping b and c variables b = b + c; c = b - c; b = b - c; // swapping c and d variables c = c + d; d = c - d; c = c - d; Console.WriteLine( "values after " + "swapping are : " ); Console.WriteLine( "a = " + a); Console.WriteLine( "b = " + b); Console.WriteLine( "c = " + c); Console.WriteLine( "d = " + d); } // Driver Code public static void Main() { // initialising variables int a = 1; int b = 2; int c = 3; int d = 4; Console.WriteLine( "values before " + "swapping are : " ); Console.WriteLine( "a = " + a); Console.WriteLine( "b = " + b); Console.WriteLine( "c = " + c); Console.WriteLine( "d = " + d); Console.WriteLine( "" ); // Function call swap(a, b, c, d); } } // This code is contributed by Smitha. |
PHP
<?php // PHP program to swap 4 variables // without using temporary variable function swap( $a , $b , $c , $d ) { // swapping a and b variables $a = $a + $b ; $b = $a - $b ; $a = $a - $b ; // swapping b and c variables $b = $b + $c ; $c = $b - $c ; $b = $b - $c ; // swapping c and d variables $c = $c + $d ; $d = $c - $d ; $c = $c - $d ; echo "values after swapping are : " , "" ; echo "a = " , $a , "" ; echo "b = " , $b , "" ; echo "c = " , $c , "" ; echo "d = " , $d , "" ; } // Driver Code // initialising variables $a = 1; $b = 2; $c = 3; $d = 4; echo "Values before swapping are :" , "" ; echo "a = " , $a , "" ; echo "b = " , $b , "" ; echo "c = " , $c , "" ; echo "d = " , $d , "" , "" ; // Function call swap( $a , $b , $c , $d ); // This code is contributed by aj_36 ?> |
Javascript
<script> // Javascript program to swap 4 variables // without using temporary variable. function swap(a, b, c, d) { // swapping a and b variables a = a + b; b = a - b; a = a - b; // swapping b and c variables b = b + c; c = b - c; b = b - c; // swapping c and d variables c = c + d; d = c - d; c = c - d; document.write( "values after swapping are : " + "</br>" ); document.write( "a = " + a + "</br>" ); document.write( "b = " + b + "</br>" ); document.write( "c = " + c + "</br>" ); document.write( "d = " + d); } // initialising variables let a = 1; let b = 2; let c = 3; let d = 4; document.write( "values before swapping are : " + "</br>" ); document.write( "a = " + a + "</br>" ); document.write( "b = " + b + "</br>" ); document.write( "c = " + c + "</br>" ); document.write( "d = " + d + "</br>" ); document.write( "" + "</br>" ); // Function call swap(a, b, c, d); </script> |
输出
Values before swapping are :a = 1b = 2c = 3d = 4values after swapping are : a = 2b = 3c = 4d = 1
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END