当前位置:网站首页>Four methods of exchanging the values of a and B
Four methods of exchanging the values of a and B
2022-07-06 14:38:00 【Wang yeqiang】
In exchange for a,b Value , You may think it's very simple , But if you suddenly think of 4 Methods , Maybe the most basic method of temporary storage with the third variable appears in your mind for a moment , Now I summarize four methods :
initialization a and b:
# About python Replace a b It's worth it Method
a = 3
b = 5
print(" At the beginning of the :a = {} , b = {}".format(a,b))
1. Use the third variable c The staging :
def ChangeFirst(a,b): # Take the third variable C The staging
c = a
a = b
b = c
print("ChangeFirst After method exchange :a = {} , b = {}".format(a,b))
2. Exchange sum and difference ( The staging ):
def ChangeSecond(a,b): # Use sum and difference
a = a+b
b = a-b
a = a-b
print("ChangeSecond After method exchange :a = {} , b = {}".format(a,b))
3. Using bitwise XOR :
def ChangeThird(a,b): # Bitwise XOR ( In binary , Same as 0 Different for 1)
a = a^b
b = a^b
a = a^b
print("ChangeThird After method exchange :a = {} , b = {}".format(a,b))
4.Python Proprietary approach :
def ChangeFourth(a,b): #python Proprietary approach
a,b = b,a
print("ChangeFourth After method exchange :a = {} , b = {}".format(a,b))
Code runs :
ChangeFirst(a,b)
ChangeSecond(a,b)
ChangeThird(a,b)
ChangeFourth(a,b)
Code output :
边栏推荐
- Pointer -- eliminate all numbers in the string
- On the idea of vulnerability discovery
- 数字电路基础(一)数制与码制
- 安全面试之XSS(跨站脚本攻击)
- [pointer] find the value of the largest element in the two-dimensional array
- Feature extraction and detection 14 plane object recognition
- SQL injection
- Pointers: maximum, minimum, and average
- [pointer] the array is stored in reverse order and output
- 数字电路基础(四) 数据分配器、数据选择器和数值比较器
猜你喜欢
Intranet information collection of Intranet penetration (4)
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
Wu Enda's latest interview! Data centric reasons
《统计学》第八版贾俊平第十四章指数知识点总结及课后习题答案
线程的实现方式总结
关于超星脚本出现乱码问题
1.支付系统
数字电路基础(四) 数据分配器、数据选择器和数值比较器
Keil5-MDK的格式化代码工具及添加快捷方式
Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
随机推荐
What language should I learn from zero foundation. Suggestions
Database monitoring SQL execution
《统计学》第八版贾俊平第九章分类数据分析知识点总结及课后习题答案
MSF generate payload Encyclopedia
Function: find the root of the equation by Newton iterative method
Library management system
Chain team implementation (C language)
数据库多表链接的查询方式
This article explains in detail how mockmvc is used in practical work
《统计学》第八版贾俊平第十三章时间序列分析和预测知识点总结及课后习题答案
Record an API interface SQL injection practice
2022华中杯数学建模思路
Pointer -- output all characters in the string in reverse order
On the idea of vulnerability discovery
Based on authorized access, cross host, and permission allocation under sqlserver
"If life is just like the first sight" -- risc-v
Attack and defense world misc practice area (GIF lift table ext3)
Windows platform mongodb database installation
《統計學》第八版賈俊平第七章知識點總結及課後習題答案
【指针】查找最大的字符串