方法
- 采用第三方变量
- 采用减法
- 采用按位异或
具体实现
#include<stdio.h>
int main(){
//交换两个变量的值
int a = 3;
int b = 8;
int c = a;
printf("初始化值\n");
printf("a=%d\n",a);
printf("b=%d\n",b);
//1.采用第三方变量
a = b;
b = c;
printf("1.第三方变量交换后的值\n");
printf("a=%d\n",a);
printf("b=%d\n",b);
//2.采用减法进行值交换
b = a -b;
a = a -b;
b = a + b;
printf("2.采用减法进行值交换后的值\n");
printf("a=%d\n",a);
printf("b=%d\n",b);
//3.采用按位异或的位方式
a = a^b;
b = a^b;
a = a^b;
printf("3.采用按位异或的位方式\n");
printf("a=%d\n",a);
printf("b=%d\n",b);
return 0;
}