当前位置:网站首页>Several ways to exchange two variable values without the third variable
Several ways to exchange two variable values without the third variable
2022-04-22 05:51:00 【atwdy】
Variable a=10,b=20, Realize the exchange of two variable values without the help of the third variable .
1、 Arithmetic operations
I saw this problem a long time ago , At that time, I only knew that it was XOR ^ solve , Later, I also saw that someone used addition and subtraction or multiplication and division to solve , This arithmetic operation is easy to understand, and the code is posted below .
Add and subtract to achieve :
a = 10
b = 20
a = a + b
b = a - b
a = a - b
print(a, b) # 20 10
Multiplication and division implementation :
a = 10
b = 20
a = a * b
b = a / b
a = a / b
print(a, b) # 20.0 10.0 The decimal point is because python The result of middle division is floating point
2、 Exclusive or (^)
A simple overview of XOR is that the same is false (0), Difference is true (1).
The key point of XOR operation is : hypothesis a^b=c, be c^b=a,c^a=b, That is to say, a number is exclusive or the same number twice, and the result remains the same .
We all know that the data in the computer is stored in binary form , Now let's assume two variables at random a, b Binary system ,a=101101,b=001010,a^b^b=a^(b^b), And according to the definition of XOR , Any number and its own XOR will be equal to 0, For example, the above b^b=001010^001010=000000. Also according to the definition of XOR , Any number and 0 XOR will be equal to the number itself , It's like a^0=101101^000000=101101.
Figured out the logic of XOR , The code is easy to write
# XOR implementation
a = 10
b = 20
a = a ^ b
b = a ^ b
a = a ^ b
print(a, b) # 20 10
3、python Unpacking in
a = 10
b = 20
a, b = b, a
print(a, b) # 20 10
版权声明
本文为[atwdy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220537014051.html
边栏推荐
- Simple DP questions - cow breeding and super stair climbing
- Why introduce collaborative process
- Exploration des données - regroupement
- TCGA下载GBM患者的RNA-seq数据
- 01 knapsack problem (template)
- ‘PdfFileWriter‘ object has no attribute ‘stream‘
- torch 循环神经网络torch.nn.RNN()和 torch.nn.RNNCell()
- 《最优化理论》:运输问题(一)求最小运费【西北角法、最小元素法、伏格尔法】
- LeetCode 589. N 叉树的前序遍历
- 全排列(回溯法)模板
猜你喜欢

LeetCode 99. 恢复二叉搜索树 -- 中序遍历+排序

LeetCode 486. 预测赢家--动态规划+博弈论

LeetCode 2049. 统计最高分的节点数目--树的遍历

Common methods of arrays (super detailed explanation)

Complete knapsack problem

The breakpoint will not currently be hit No symbols have been loaded for this document.

伪代码块编写(论文编写用)

机器学习——用鸢尾花数据集画P-R曲线和ROC曲线

Goodbye 20202021 I'm coming

C語言--經典100題
随机推荐
7-10 最长对称子串 (25 分)(暴力题解)C语言
uniapp:HBuilderX运行uniapp项目到夜神模拟器
整数拆分问题(动态规划+递归&记录数组)
opencv 骨架提取/图片细化 代码
判断链表是否有环(集合&快慢指针)
LeetCode 486. 预测赢家--动态规划+博弈论
TCGA数据库ensembl id 转为 gene Symbol,提取出需要的RNA种类表达谱列表信息
什么是JSON?初识JSON
Data mining -- association rule mining
raspberry keras-ocr can‘t allocate memory in static TLS block
01 knapsack problem (template)
MySQL functions and exercises (II)
Digital triangle (dynamic programming DP)
Data mining - logistic regression
Data mining -- data preprocessing
excel根据单元格内容设定行列颜色
机器学习——用鸢尾花数据集画P-R曲线和ROC曲线
根源:pip终端下载的包import不能用
數據挖掘——聚類
Data mining -- naive Bayesian classification