1.临时变量法
temp=a;
a=b;
b=temp;
2.加减运算法
a=a+b;
b=a-b;
a=a-b;
3.位运算法
a=a^b;
b=b^a;
a=a^b;
(等价于a^=b^=a^=b;)
4.指针法
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}
时间: 11-15
1.临时变量法
temp=a;
a=b;
b=temp;
2.加减运算法
a=a+b;
b=a-b;
a=a-b;
3.位运算法
a=a^b;
b=b^a;
a=a^b;
(等价于a^=b^=a^=b;)
4.指针法
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
}