当前位置:网站首页>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 :
边栏推荐
- C language learning summary (I) (under update)
- 《统计学》第八版贾俊平第四章总结及课后习题答案
- 关于交换a和b的值的四种方法
- How to test whether an object is a proxy- How to test if an object is a Proxy?
- “Hello IC World”
- 《统计学》第八版贾俊平第八章假设检验知识点总结及课后习题答案
- Fire! One day transferred to go engineer, not fire handstand sing Conquest (in serial)
- Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
- Function: find 1-1/2+1/3-1/4+1/5-1/6+1/7-... +1/n
- 我的第一篇博客
猜你喜欢
Uibutton status exploration and customization
Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]
数字电路基础(四) 数据分配器、数据选择器和数值比较器
Hcip -- MPLS experiment
How does SQLite count the data that meets another condition under the data that has been classified once
Statistics 8th Edition Jia Junping Chapter 14 summary of index knowledge points and answers to exercises after class
Based on authorized access, cross host, and permission allocation under sqlserver
Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary
1.支付系统
线程的实现方式总结
随机推荐
Intranet information collection of Intranet penetration (I)
Statistics, 8th Edition, Jia Junping, Chapter 6 Summary of knowledge points of statistics and sampling distribution and answers to exercises after class
《统计学》第八版贾俊平第十章方差分析知识点总结及课后习题答案
Library management system
循环队列(C语言)
c语言学习总结(上)(更新中)
Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
函数:求方程的根
Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary
JDBC事务、批处理以及连接池(超详细)
“Hello IC World”
On the idea of vulnerability discovery
《统计学》第八版贾俊平第八章假设检验知识点总结及课后习题答案
. Net6: develop modern 3D industrial software based on WPF (2)
Intel oneapi - opening a new era of heterogeneity
Chain team implementation (C language)
Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
Pointer -- output all characters in the string in reverse order
移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator