当前位置:网站首页>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
- 《統計學》第八版賈俊平第七章知識點總結及課後習題答案
- Database monitoring SQL execution
- “Hello IC World”
- Wu Enda's latest interview! Data centric reasons
- Intranet information collection of Intranet penetration (4)
- Transplant hummingbird e203 core to Da Vinci pro35t [Jichuang xinlai risc-v Cup] (I)
- Solutions to common problems in database development such as MySQL
- Detailed explanation of network foundation
猜你喜欢

移植蜂鸟E203内核至达芬奇pro35T【集创芯来RISC-V杯】(一)

Attack and defense world misc practice area (GIF lift table ext3)

线程的实现方式总结

Circular queue (C language)

Interview Essentials: what is the mysterious framework asking?

Detailed explanation of network foundation

《统计学》第八版贾俊平第七章知识点总结及课后习题答案

《统计学》第八版贾俊平第十章方差分析知识点总结及课后习题答案

数字电路基础(一)数制与码制

JDBC read this article is enough
随机推荐
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
[pointer] solve the last person left
《统计学》第八版贾俊平第二章课后习题及答案总结
High concurrency programming series: 6 steps of JVM performance tuning and detailed explanation of key tuning parameters
Library management system
Feature extraction and detection 14 plane object recognition
Numpy快速上手指南
王爽汇编语言详细学习笔记二:寄存器
Solutions to common problems in database development such as MySQL
安全面试之XSS(跨站脚本攻击)
Intranet information collection of Intranet penetration (4)
New version of postman flows [introductory teaching chapter 01 send request]
Always of SystemVerilog usage_ comb 、always_ iff
Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]
JDBC事务、批处理以及连接池(超详细)
5 minutes to master machine learning iris logical regression classification
[issue 18] share a Netease go experience
Get started with Matplotlib drawing
Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
Statistics 8th Edition Jia Junping Chapter 14 summary of index knowledge points and answers to exercises after class