首页 > 代码库 > 不用中间变量交换两个数 swap(a,b);

不用中间变量交换两个数 swap(a,b);

#include <iostream>using namespace std;int main () {    int a = 3;    int b = 5;    cout<<"a="<<a<<",b="<<b<<endl;    a = a+b;    ///a=7    b = a-b;    ///b=3;    a = a-b;    ///a=5    cout<<"a="<<a<<",b="<<b<<endl;    return 0;}

输出:

a=3,b=5

a=5,b=3

不用中间变量交换两个数 swap(a,b);