技巧:对于两个整数x,y,有x^x^y = x^y^x = y^x^x = y,^代表异或。异或:两个位的值不相同,则结果为1,如果两个位值相同,则结果为0。
如 2^2^3 = 2^3^2 = 3^2^2 = 2
所以其C++代码如下:
#include <iostream>//输出结果为:x = 3 , y = 2
int main()
{
int x = 2, y = 3;
x = x^y;
y = x^y;
x = x^y;
std::cout<<"x = "<<x<<" , y = "<<y<<std::endl;
return 0;
}