如何实现两个数值不通过中间变量直接互换?
- 内容介绍
- 文章标签
- 相关推荐
本文共计117个文字,预计阅读时间需要1分钟。
通过调整数值或条件,逐步缩小搜索范围,找到最佳解决方案的方法。
加减法
#include<stdio.h>
int main(){
int a =3;
int b=5;
a=a+b;
b=a-b;
a=a-b;
printf("%d\n%d",a,b);
return 0;
}
利用^(按位异或)
#include<stdio.h>//
int main(){
int a =3;
int b=5;
a=a^b;
b=a^b;
a=a^b;
printf("%d\n%d\n",a,b);
return 0;
}
本文共计117个文字,预计阅读时间需要1分钟。
通过调整数值或条件,逐步缩小搜索范围,找到最佳解决方案的方法。
加减法
#include<stdio.h>
int main(){
int a =3;
int b=5;
a=a+b;
b=a-b;
a=a-b;
printf("%d\n%d",a,b);
return 0;
}
利用^(按位异或)
#include<stdio.h>//
int main(){
int a =3;
int b=5;
a=a^b;
b=a^b;
a=a^b;
printf("%d\n%d\n",a,b);
return 0;
}

