Home > Backend Development > PHP Tutorial > Classic algorithm learning - exchanging two integer data_PHP tutorial

Classic algorithm learning - exchanging two integer data_PHP tutorial

WBOY
Release: 2016-07-12 08:54:04
Original
911 people have browsed it

Classic algorithm learning - exchanging two integer data

Exchanging two numbers is often used in programming. Of course, we can do it in very common ways, or we can do it in various weird ways. Here we use three more conventional ways to do it. I personally feel that too weird methods are not necessary. Upload the example code to: https://github.com/chenyufeng1991/SwapFunction

(1) Using pointers

The implementation is as follows:

//
//  main.c
//  SwapFunc
//
//  Created by chenyufeng on 16/2/3.
//  Copyright © 2016年 chenyufengweb. All rights reserved.
//

#include <stdio.h>

void swap01(int *a,int *b);

int main(int argc, const char * argv[]) {

    int a = 1;
    int b = 2;
    printf("交换前:a = %d,b = %d\n",a,b);
    swap01(&a, &b);
    printf("交换后:a = %d,b = %d\n",a,b);

    return 0;
}

//最常规的交换;
void swap01(int *a,int *b){

    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}
</stdio.h>
Copy after login
(2) Do not borrow the third number
//
//  main.c
//  SwapFunc
//
//  Created by chenyufeng on 16/2/3.
//  Copyright &copy; 2016年 chenyufengweb. All rights reserved.
//

#include <stdio.h>

void swap02(int *a,int *b);

int main(int argc, const char * argv[]) {

    int a = 1;
    int b = 2;
    printf("交换前:a = %d,b = %d\n",a,b);
    swap02(&a, &b);
    printf("交换后:a = %d,b = %d\n",a,b);

    return 0;
}

//不用第三个数;
void swap02(int *a,int *b){

    *a = *a + *b;
    *b = *a - *b;
    *a = *a - *b;
}
</stdio.h>
Copy after login

(3) XOR

//
//  main.c
//  SwapFunc
//
//  Created by chenyufeng on 16/2/3.
//  Copyright &copy; 2016年 chenyufengweb. All rights reserved.
//

#include <stdio.h>

/**
 *  由于我这里用的是C语言,所以不能使用引用。C++中可以使用引用。
 引用的函数定义:
 void swap04(int &a,int &b){
 ...
 }
 */

void swap03(int *a,int *b);

int main(int argc, const char * argv[]) {

    int a = 1;
    int b = 2;
    printf("交换前:a = %d,b = %d\n",a,b);
    swap03(&a, &b);
    printf("交换后:a = %d,b = %d\n",a,b);

    return 0;
}

//异或,使用二进制位进行计算;
void swap03(int *a,int *b){

    *a = *a ^ *b;
    *b = *b ^ *a;
    *a = *a ^ *b;
}
</stdio.h>
Copy after login
You should be able to write the above three implementations with your eyes closed, and you can fully understand them.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1121831.htmlTechArticleClassic algorithm learning - exchanging two integer data exchanging two numbers is often used in programming , of course we can do it in very common ways, or in various weird ways...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template