#include<stdio.h>
void Swap1(int x, int y)//使用了中间值
{
int temp;
temp = x;
x = y;
y = temp;
printf("第一种%d,%d\n",x,y);
}
void Swap2(int x, int y)//未使用中间值
{
x = x + y;
y = x - y;
x = x - y;
printf("第二种%d,%d\n",x,y);
}
void Swap3(int x, int y)//使用了异或关系(高级)
{
x = x^y;//相当于x=x-y;
y = x^y;//相当于y=x+y;
x = x^y;//相当于x=y-x;
printf("第三种%d,%d\n",x,y);
}
int main()
{
int a = 10;
int b = 20;
printf("%d,%d\n",a,b);
printf("交换之后:\n");
Swap1(a, b);
Swap2(a, b);
Swap3(a, b);
system("pause");
return 0;
}
原文地址:https://blog.51cto.com/14233078/2382979
时间: 2024-10-09 01:57:14