不用中间变量交换两个数 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

posted on 2014-09-16 17:59  旭东的博客  阅读(1591)  评论(2编辑  收藏  举报

导航