Using C language to realize numerical exchange

Source: Internet
Author: User

My first blog, I want to start with the simplest. I am a student, today brought to you with C language to achieve two number of numeric exchange, if you have any questions or suggestions, you can give me a message. We discuss together to study, thank you! 650) this.width=650; "src=" Http://img.baidu.com/hi/jx2/j_0002.gif "alt=" J_0002.gif "/>

There are three ways to solve the problem of numerical Exchange:

(a) Set a third variable to help with numeric exchange.

Idea: Like two cups are filled with liquid, a cup of red liquid, B cup in the blue liquid, you need an empty cup C, the liquid A in the C, so that a cup empty, to the B Cup of liquid into a, and finally the C Cup of liquid into the B Cup. The liquid in the last a cup is blue (from the B Cup) and the liquid in the B Cup is red (from a).

#include <stdio.h>
int main ()
 {
int a = 9, B = +, c = 0;//exchange A and B values, set a new variable C, and initialize to 0
C = A; c = 9 gives the value of a to C first
a = b; A = the value of B is given a
B = C; b = The value of 9 C gives B
printf ("%d%d", A, b); A = 14,b = 9
return 0;
 }

The above is the first method to complete the numerical exchange.

(ii) numerical Exchange by addition or multiplication

Before we set the third variable, starting with this method, we can no longer set a new variable.

Idea: We use addition as an example for numerical exchange, let's say we exchange the values of A and B, a = 9, B = 14, first we add the value of a and B to a + b = 23, then we assign the value of 23 to a (i.e. A + B), so now a = 23, then the value of 23 minus B is the value of a, but we need to A is given a value of B, so B = A-B (that is, B = (A + b)-C = a), the value of a is given B, and finally a new value of B is subtracted from 23, which becomes the value of a (that is, a = 23-b). Minus the new value of B, it becomes the value of a (that is, a = 23-b).

#include <stdio.h>
int main ()
{
int a = 9,b = 14;
A = a + B; The a value to the left of the equals sign is 23, the a value to the right of the equals sign is 9, and the value of a after the statement ends at
B = a A; A value of 23,b to the right of the equal sign is the original value 14, 23-14 = 9, which gives the B (the original a value) to the left of the equals sign
A = a-B; A value to the right of the equal sign is 23,b value of 12,23-9=14, and a (original B value) to the left of the equal sign .
printf ("%d%d", A, b); A = +, B = 9
return 0;
}

The above is a numerical exchange with addition, the idea of multiplication and addition is the same, just the "+" in the program to the "*" number, the "-" number to "/". However, when using this method, it is important to note that if the value is too large, it may overflow.

(iii) Exchange of values by means of an XOR method

Idea: First let's look at the different or

A = 9, B = 14

A is a value of 9, then we write its binary code is the value of 1001,b is 14, it corresponds to the binary encoding is 1110, XOR is to two number of binary encoded corresponding bits to compare, not the same output 1, the same output 0.

A = 9 1 0 0 1

b = 14 1 1 1 0

The result of the comparison is 0 1 1 1

Let's remember 0 1 1 1 this number.

Then we use the value of a of 9 corresponding binary code 1 0 0 1 to go with 0 1 1 1 XOR:

A = 9 1 0 0 1

0 1 1 1

The comparison result is 1 1 1 0 (that is, the value of B)

Similarly we use the value of B 14 for the binary code 1 1 1 0 to go with 0 1 1 1 XOR:

b = 14 1 1 1 0

0 1 1 1

The comparison result is 1 0 0 1 (that is, the value of a)

Believe us to see the law, that is, the value of a and B binary code xor out of the number, a and that number XOR get b,b and that number XOR or get a.

#include <stdio.h>
int main ()
{
int a = 9,b = 14; A 1001 (9) B 1110 (+)
a = a^b; A = 1001 ^ 1110 = 0111
B = a^b; b = 0111 ^ 1110 = 1001 (9)
a = a^b; A = 0111 ^ 1001 = 1110 (+)
printf ("%d%d", A, b);
return 0;
}

The above is a numerical exchange by means of an XOR method, which solves the problem that the second method may overflow due to a large number, but this method is less efficient.

The above is a numerical exchange of three methods, if you have any other good method, we can discuss learning together, thank you!


This article is from the "10944002" blog, please be sure to keep this source http://10954002.blog.51cto.com/10944002/1738418

Using C language to realize numerical exchange

Related Article

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.