当前位置:网站首页>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 :

边栏推荐
- Sword finger offer 23 - print binary tree from top to bottom
- Bing Dwen Dwen official NFT blind box will be sold for about 626 yuan each; JD home programmer was sentenced for deleting the library and running away; Laravel 9 officially released | Sifu weekly
- Harmonyos application development -- address book management system telmanagesys based on listcontainer [phonebook][api v6]
- Mathematical modeling idea of 2022 central China Cup
- 后台登录系统,JDBC连接数据库,做小案例练习
- Constants, variables, and operators of SystemVerilog usage
- 【指针】数组逆序重新存放后并输出
- captcha-killer验证码识别插件
- 《統計學》第八版賈俊平第七章知識點總結及課後習題答案
- 《统计学》第八版贾俊平第十四章指数知识点总结及课后习题答案
猜你喜欢

“Hello IC World”

"If life is just like the first sight" -- risc-v

Data mining - a discussion on sample imbalance in classification problems

JDBC transactions, batch processing, and connection pooling (super detailed)

ES全文索引

数字电路基础(四) 数据分配器、数据选择器和数值比较器

《统计学》第八版贾俊平第九章分类数据分析知识点总结及课后习题答案

循环队列(C语言)

Proceedingjoinpoint API use

Solutions to common problems in database development such as MySQL
随机推荐
四元数---基本概念(转载)
1.支付系统
关于交换a和b的值的四种方法
Fundamentals of digital circuit (IV) data distributor, data selector and numerical comparator
[pointer] find the value of the largest element in the two-dimensional array
On the idea of vulnerability discovery
5分钟掌握机器学习鸢尾花逻辑回归分类
captcha-killer验证码识别插件
This article explains in detail how mockmvc is used in practical work
【指针】查找最大的字符串
Intranet information collection of Intranet penetration (3)
Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
{1,2,3,2,5}查重问题
JDBC 的四种连接方式 直接上代码
Record an edu, SQL injection practice
Windows platform mongodb database installation
函数:字符串反序存放
《统计学》第八版贾俊平第二章课后习题及答案总结
Realize applet payment function with applet cloud development (including source code)
后台登录系统,JDBC连接数据库,做小案例练习