当前位置:网站首页>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 :
边栏推荐
- Chain team implementation (C language)
- 函数:计算字符串中大写字母的个数
- “Hello IC World”
- 5 minutes to master machine learning iris logical regression classification
- Intranet information collection of Intranet penetration (I)
- Mathematical modeling idea of 2022 central China Cup
- Attack and defense world misc practice area (GIF lift table ext3)
- 《统计学》第八版贾俊平第五章概率与概率分布
- How to earn the first pot of gold in CSDN (we are all creators)
- Pointers: maximum, minimum, and average
猜你喜欢
Statistics 8th Edition Jia Junping Chapter XIII Summary of knowledge points of time series analysis and prediction and answers to exercises after class
Get started with Matplotlib drawing
《统计学》第八版贾俊平第十章方差分析知识点总结及课后习题答案
Statistics 8th Edition Jia Junping Chapter 3 after class exercises and answer summary
Interview Essentials: what is the mysterious framework asking?
链队实现(C语言)
Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution
王爽汇编语言详细学习笔记二:寄存器
四元数---基本概念(转载)
Attack and defense world misc practice area (GIF lift table ext3)
随机推荐
New version of postman flows [introductory teaching chapter 01 send request]
captcha-killer验证码识别插件
《统计学》第八版贾俊平第七章知识点总结及课后习题答案
What language should I learn from zero foundation. Suggestions
The difference between layer 3 switch and router
函数:求两个正数的最大公约数和最小公倍
Solutions to common problems in database development such as MySQL
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
On the idea of vulnerability discovery
Circular queue (C language)
XSS (cross site scripting attack) for security interview
王爽汇编语言详细学习笔记二:寄存器
Intranet information collection of Intranet penetration (3)
函数:求1-1/2+1/3-1/4+1/5-1/6+1/7-…+1/n
“人生若只如初见”——RISC-V
指针:最大值、最小值和平均值
Intranet information collection of Intranet penetration (2)
SystemVerilog discusses loop loop structure and built-in loop variable I
【指针】求字符串的长度
{1,2,3,2,5}查重问题