两个数交换的四种方法

1,

        int x = 7; 
        int y = 3; 
        int t;
        t=x;
        x=y;
        y=t;    

 

2,

        int x = 7; 
        int y = 3; 
        x = x + y;
        y = x - y;
        x = x - y;
           

3,

        int x = 7; //0111
        int y = 3; //0011
        x = x^y;//0100 
        y = x^y;//0111 = 7
        x = x^y;//0011 = 3

4,

    int a = 3;
    int b = 4;
    a = (a + b) - (b = a);    

 

posted @ 2022-08-02 22:53  风子磊  阅读(122)  评论(0编辑  收藏  举报