Exchange two numbers without intermediate variables (Java version)

Source: Internet
Author: User
This article is original. If you need to reprint it, please indicate the author and the source. Thank you!

In Program It is not complicated to implement the function of exchanging two numbers, but if you do not use the intermediate variable, you need to use your mind. This article introduces two methods (in fact, the principle is one ). The basic principle is the number neutralization. That is to say, a certain operation (binary operation) converts the numbers A and B into one and stores them in one of the variables. Then, use the same operator to neutralize A or B. In fact, A or B is used as an intermediate variable.
FirstAlgorithm . Static   Class Num
{
Int A;
Int B;
}
Public   Static   Void Swap1 (Num)
{

Num.=Num.+Num. B;
Num. B
=Num.-Num. B;
Num.
=Num.-Num. B;
}

AboveCodeThe "+" operator assigns the calculation results of A and B to a (then A is an intermediate variable ). Then calculate B. Then, the value of a is (a + B). Therefore, A minus B is the original. At this time, B is already the original A. Therefore, use the calculated a (actually a + B) to subtract the computed B (actually the original ), B is the original B. Finally, assign B to.
In fact, we can also use symbols such as "*" and "/" to achieve the same effect. The Code is as follows:

Public Static Void Swap2 (Num)
{
Num.
= Num. * Num. B;
Num. B
= Num. / Num. B;
Num.
= Num. / Num. B;
}
Public Static Void Swap3 (Num)
{
Num.
= Num. - Num. B;
Num. B
= Num. + Num. B;
Num.
= Num. B - Num.;
}

The above Code has no problem in Java (but when "/" is used, the denominator and numerator cannot be 0 ). If overflow occurs, the system returns the correct result. However, in some languages (such as C), overflow errors may be thrown, which cannot be avoided. The Code is as follows: Public Static Void Swap4 (Num)
{
// Different symbols
If (Num. * Num. B <= 0 )
{
Num.
= Num. + Num. B;
Num. B
= Num. - Num. B;
Num.
= Num. - Num. B;
}
Else
{

Num.=Num.-Num. B;
Num. B
=Num.+Num. B;
Num.
=Num. B-Num.;
}
}

Of course, we also have a better way, that is, using the XOR operator. We know that any number is equal to 0 or is still itself, and two identical numbers are different or followed by 0. The following code is applicable to this feature.

Public Static Void Swap5 (Num)
{
Num.
= Num. ^ Num. B;
Num. B
= Num. ^ Num. B;
Num.
= Num. ^ Num. B;
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.