About how to exchange two numbers

1. Given the values ​​of two integer variables, exchange the contents of the two values.

Idea: In a computer, it is not like the human brain that can directly exchange the contents of two variables, but we can temporarily define an intermediate variable and use the intermediate variable to exchange the contents of the two variables.

//An highlighted blockvar foo = 'bar';
#include<stdio.h>
int main()
{
    
    
  int i=62, j=24, t=0;
  printf("%d  %d\n", i, j);
  t = j;
  j = i;
  i = t;
  printf("After change :%d  %d\n", i, j);
  system("pause");
  return 0;
}

Successful exchange

2. It is not allowed to create temporary variables and exchange the contents of two numbers

1. Idea: You can use the characteristics of mathematics to exchange. The sum of two numbers minus one of them is the other number.

//An highlighted blocked foo = 'bar';
#include<stdio.h>
int main()
{
    
    
 int i = 62, j = 24;
 printf("%d  %d\n", i, j);
 i = i+j;
 j = i-j;
 i = i-j;
 printf("After change :%d  %d\n", i, j);
 system("pause");
 return 0;
}

Disadvantages: In addition and subtraction, if the number is too large, it will cause an overflow, that is, the values ​​of a and b are limited.

2. Thinking switch: According to the characteristics of binary, the value of i ^ j is assigned to i, so that j=i ^ j is equivalent to the original i ^ j ^ j, so the value of i is assigned to j; the
same In principle, in the next step, i=i ^ j is equivalent to the original i ^ j ^ i, so that the value of j is assigned to i; in this way, the exchange of values ​​can also be achieved without too many limitations.

//An highlighted blocked foo = 'bar';
#include<stdio.h>
int main()
{
    
    
 int i = 62, j = 24;
 printf("%d  %d\n", i, j);
 i = i^j;
 j = i^j;
 i = i^j;
 printf("After change :%d  %d\n", i, j);
 system("pause");
 return 0;
}

But in actual programming, it is more convenient and quicker to use intermediate variables.

3. Exchange the contents of array A with the contents of array B. (The array is the same size)

Idea: Create two arrays a and b, and assign values ​​in the array, use a loop to traverse the values ​​in the array, and exchange the two values ​​of a and b through the above 1. this is very simple!
So can it be more difficult to call the latter function establishment function?
First look at a piece of code:

//An highlighted blockvar foo = 'bar';
#include 
void Swap1(int x, int y)
{
    
    
 int tmp = 0;
 tmp = x;
 x = y;
 y = tmp;
}
void Swap2(int *px, int *py)
{
    
    
 int tmp = 0;
 tmp = *px;
 *px = *py;
 *py = tmp;
}
int main()
{
    
    
 int num1 = 1;
 int num2 = 2;
 Swap1(num1, num2);
 printf("Swap1::num1 = %d num2 = %d\n", num1, num2);
 Swap2(&num1, &num2);
 printf("Swap2::num1 = %d num2 = %d\n", num1, num2);
 return 0;
}

Insert picture description here
Obviously, it's not possible to exchange values ​​directly. Need to use pointers, pass-by-address pass-through parameters so that you can exchange the value of the array.

//An highlighted blockvar foo = 'bar';
#pragma warning(disable:4996)
#include<stdio.h>
int exch(int *arr1[10], int *arr2[10])
{
    
    
 int j;
 int t;
 for (j = 0; j < 10; j++)
 {
    
    
  t = arr1[j];
  arr1[j] = arr2[j];
  arr2[j] = t;
 }
}
int main()
{
    
    
 int a[10], b[10];
 int i;
 printf("please input a:\n");
 for (i = 0; i < 10; i++){
    
    
  scanf("%d", &a[i]);
 }
 printf("please input b:\n");
 for (i = 0; i < 10; i++) {
    
    
  scanf("%d", &b[i]);
 }
 exch(&a, &b);
 printf("after exchange a:");
 for (i = 0; i < 10; i++)
 {
    
    
  printf("%d\t", a[i]);
 }
 printf("\nafter exchange b:");
 for (i = 0; i < 10; i++)
 {
    
    
  printf("%d\t", b[i]);
 }
 system("pause");
 return 0;
}

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_40893595/article/details/90182086