1、加减思想
#include<stdio.h>
int main()
{
int a = 3, b = 5;
printf("交换前:a=%d b=%d\n",a,b);
a = a + b;
b = a - b;
a = a - 3;
printf("交换后:a=%d b=%d\n", a, b);
return 0;
}
存在问题:整形溢出
2、按位异或(二进制)
int main()
{
int a = 3, b = 5;
printf("交换前:a=%d b=%d\n", a, b);
a = a ^ b;
b = a ^ b;
a = a ^ b;
printf("交换后:a=%d b=%d\n", a, b);
return 0;
}
不会溢出,且不使用第三个变量
标签:题目,变量,int,交换,第三个,printf,return,main From: https://www.cnblogs.com/tingting--123/p/17770292.html