Interview Questions → exchange the values of two variables

Source: Internet
Author: User

 

Interview Questions: Exchange two variables
Maybe when you hear this question, you will secretly laugh. Is this simple algorithm question actually taken out as a face-to-face question? Are you also skeptical about the company's own capabilities and scale.
Exchange two variables: for example, num1 = 5, num2 = 6 → num1 = 6, num = 5
Do you think it is very simple? Do you use the following method to do it:
Int num1 = 5;
Int num2 = 6;
Int temp = num1;
Num1 = num2;
Num2 = temp;
Console. writeline ("num1 = {0}, num2 = {1}", num1, num2 );
Right. You have learned the role of intermediate variables since learning programming. For example, two bottles, one bottle is Cola and the other bottle is Sprite, if you want to exchange the drinks in two bottles, the first thing we think of is to use the intermediate variable (then find an empty bottle) first, pour one of the drinks (sprite or cola) into an empty bottle, and then another drink (cola or sprite) into the bottle that has just been poured out, finally, I gave the drink in the bottle used as the intermediate variable to the empty bottle, so that I could exchange two drinks.
This method is no longer logical.


Next let's take a look at the following interview questions:

No intermediate variable is allowed to exchange values of two variables: num1 = 5, num2 = 6

What do you mean by a few minutes?

Without using any intermediate variable, the value of the variable has always been disabled for a long time. I can't help but check whether I feel that I can't help myself, let's take a look at its solution:
Int num1 = 5;
Int num2 = 6;
Num1 + = num2;
Num2 = num1-num2;
Num1 = num1-num2;
Console. writeline ("num1 = {0}, num2 = {1}", num1, num2 );


After reading the solution, will you feel suddenly enlightened? Maybe you will think that the second method is not logical, and it is not easy to send, but what can you do if you have such an interview question, one of the most important principles followed in programming is to solve the most complex problems with the simplest method. Therefore, although the latter method is not popular, it is very nonstandard, but the specific situation should be analyzed. Don't imagine how difficult it is to interview questions. In fact, they are often used. The algorithm we often see is a different form and a solution, therefore, in normal learning, we must learn knowledge and open our thinking. In this way, we will truly control what we have learned to solve new things.

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.