Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How to exchange the values of two numbers without using the third variable?
How to exchange the values of two numbers without using the third variable?
Arithmetic operation; Pointer address operation; Bit operation; Stack implementation.

The specific operation is as follows:

arithmetic operation

int a,b;

a = 10; b = 12;

a = b-a; //a = 2; b= 12

b = b-a; //a = 2; b= 10

a = b+ a; //a = 10; b= 10

Its principle is: treat A and B as points on the number axis and calculate around the distance between the two points.

Specific process: the first sentence "a=b-a" finds the distance between two points of ab and saves it in A; The second sentence "b=b-a" finds the distance from A to the origin (the difference between the distance from B to the origin and the distance from two points to ab) and saves it in B; The third sentence "a=b+a" calculates the distance from B to the origin (the sum of the distance from A to the origin and the distance from ab to the origin) and saves it in A to complete the exchange.

Compared with the standard algorithm, this algorithm has three more calculation processes, but it does not use temporary variables. (hereinafter referred to as arithmetic algorithm)

Disadvantages: it can only be used for numeric types, not for strings and the like. A+b may overflow (beyond the range of int), overflow is relative,+overflow,-just come back, so it doesn't matter if the overflow doesn't overflow, but it's not safe.

Pointer address operation

int *a,* b; //Suppose

* a = new int( 10);

* b = new int(20); //& amp; a=0x0000 1000h。 b=0x0000 1200h

a =(int *)(b-a); //& amp; a = 0x00000200h。 b=0x0000 1200h

b =(int *)(b-a); //& amp; a = 0x00000200h。 b=0x0000 1000h

a =(int *)(b+int(a)); //& amp; a=0x0000 1200h。 b=0x0000 1000h

Because the address operation is actually an integer operation, for example, two addresses are subtracted to get an integer, indicating how many bytes are separated between the storage positions of two variables in memory; Addresses are added to an integer, that is, "a+ 10" indicates the addresses of 10 class A data units with a base address. Therefore, in theory, address exchange can be completed through operations similar to arithmetic algorithms, so as to achieve the purpose of exchanging variables.

Bit operation

int a= 10,b = 12; //a= 10 10^b= 1 100;

a=a^b; //a=0 1 10^b= 1 100;

b=a^b; //a=0 1 10^b= 10 10;

a=a^b; //a = 1 100 = 12; b = 10 10;

The implementation of this algorithm is determined by the characteristics of XOR operation, which can make some bits in the data flip while others remain unchanged. This means that any number and any given value are XOR twice in succession, and the value remains the same.

Stack implementation.

int exchange(int x,int y)?

{?

Stack s; ?

push(S,x); ?

push(S,y); ?

x = pop(S); ?

y = pop(S); ?

}

All the above algorithms realize the exchange of two variable values without other variables. Comparatively speaking, the calculation amount of arithmetic algorithm and bit algorithm is equivalent, and the calculation in address algorithm is more complicated, but it can easily realize the exchange of large types (such as user-defined classes or structures), while the former two can only exchange plastic data (in theory, operators can be overloaded, and arbitrary structures can also be exchanged).