//第一种
int main()
{
int a = 5;
int b = 3;
int tmp = b;
printf("a=%d,b=%dn", a, b);
b = a;
a = tmp;
printf("a=%d,b=%dn", a, b);
return 0;
}
不创建临时变量
第二种
int main()
{
int a = 5;
int b = 3;
printf("a=%d,b=%dn", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("a=%d,b=%dn", a, b);
return 0;
}
//^异或相同为0 不同为1
int main()
{
int a = 5; //a=101
int b = 3; //b=011
printf("a=%d,b=%dn", a, b);
a = a ^ b; //a=110
b = a ^ b; //b=101
a = a ^ b; //a=011
printf("a=%d,b=%dn", a, b);
return 0;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)