/*
不使用第三个变量,进行两个变量的值交换
*/
#include <iostream>
using namespace std;
//-------------------------------------
void swap1(int& a,int& b)
{
a = a+b;
b = a-b;
a = a-b;
}
void answer1()
{
int a = 10;
int b = 20;
cout << "Before:" << a << " "<< b << endl;
swap1(a,b);
cout << "After :" << a << " "<< b << endl;
}
//-------------------------------------
void swap2(int& a,int& b)
{
a = a^b;
b = a^b;
a = a^b;
}
void answer2()
{
int a = 10;
int b = 20;
cout << "Before:" << a << " "<< b << endl;
swap2(a,b);
cout << "After :" << a << " "<< b << endl;
}
int main(int argc, char *argv[])
{
answer1();
answer2();
return 0;
}