当前位置:网站首页>Swap numbers (no temporary variables)

Swap numbers (no temporary variables)

2022-06-11 05:21:00 Food can be very delicious

That's interesting , Although the meaning of science and technology is not big , But tuyile

Title Description

Write a function , No temporary variables , Direct exchange numbers = [a, b] in a And b Value .

Ideas

Method 1

	 Add 
	a=x
	b=y
	a=a+b=x+y
	b=a-b=x+y-y=x
	a=a-b=x+y-x=y

Method 2

	 Bad 
	a=x
	b=y
	a=a-b=x-y
	b=a+b=x-y+y=x
	a=b-a=x-x+y=y

Method 3

	 Exclusive or 
	a!=b
	a^=b
	b^=a
	a^=b

A little enlightening , After all

Two identical numbers are XOR equal to 0,0 And any number exclusive or is equal to the number itself

This theorem is very useful

原网站

版权声明
本文为[Food can be very delicious]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020540512216.html