Live2D

两个变量值交换的方法

借助其他变量

引入temp暂存其中一方的值

void swap(int &a,int &b)
{
    int temp;
    temp=a;
    a=b;
    b=temp;
}

不借助其他变量

加法

void swap(int &a,int &b)
{
    a=a+b;
    b=a-b;
    a=a-b;
}

缺点:注意加法不要溢出

乘法

void swap(int &a,int &b)
{
    a=a+b;
    b=a-b;
    a=a-b;
}

缺点:注意加法不要溢出

异或

void swap(int &a,int &b)
{
    a=a^b;
    b=a^b;
    a=a^b;
}

优点:无需考虑溢出

posted @ 2020-02-18 17:18  WSquareJ  阅读(247)  评论(0编辑  收藏  举报